关于手机自带照相机内存泄露的问题


我在调用手机自带相机的时候出现了内存泄露,代码如下


 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.allowsEditing = NO;
    if (sender.tag == 1)
    {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }
    else
    {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    [imagePicker setDelegate:self];
        NSLog(@"picker retaincount1 %i",[imagePicker retainCount]);
    [self presentModalViewController:imagePicker animated:YES];
        NSLog(@"picker retaincount2 %i",[imagePicker retainCount]);

控制台输出的是


 picker retaincount1 1
picker retaincount2 4

怎么变成4了呢?求大神解决

uiimagepickercontroller imagepicker ios iphone

苦逼磨拉大 10 years, 6 months ago

这很正常, presentModalViewController 这个方法会retain传给它的view controller参数,至于retain几次,跟它内部具体实现相关。开发者没必要关心这些,只要你保证自己主动调用的alloc和release相匹配就可以了,剩下的交给系统去处理。

产气荚膜杆菌 answered 10 years, 6 months ago

Your Answer