Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIPickerController在选择照片后内存泄漏_Ios_Objective C_Memory Leaks_Uiimagepickercontroller - Fatal编程技术网

Ios UIPickerController在选择照片后内存泄漏

Ios UIPickerController在选择照片后内存泄漏,ios,objective-c,memory-leaks,uiimagepickercontroller,Ios,Objective C,Memory Leaks,Uiimagepickercontroller,每当我在图像选择器控制器中选择一张照片时,我就会发生内存泄漏。我已经试着做了我自己的研究,以了解为什么会发生这种情况,但我没有发现任何接近我的情况。我从中学到的各种教程在使用图像选择器控制器时也存在内存泄漏。我所做的一切就是选择一张照片,然后图像选择器控制器就会解散。我已经尝试寻找解决方案很多天了,但是运气不好,我倾向于在寻求帮助之前尝试寻找解决方案,非常感谢任何帮助,这是我的代码 @property (nonatomic, strong) UIImagePickerController *im

每当我在图像选择器控制器中选择一张照片时,我就会发生内存泄漏。我已经试着做了我自己的研究,以了解为什么会发生这种情况,但我没有发现任何接近我的情况。我从中学到的各种教程在使用图像选择器控制器时也存在内存泄漏。我所做的一切就是选择一张照片,然后图像选择器控制器就会解散。我已经尝试寻找解决方案很多天了,但是运气不好,我倾向于在寻求帮助之前尝试寻找解决方案,非常感谢任何帮助,这是我的代码

@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) UIImage *image;

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];


[self.tableView reloadData];
if (self.image == nil && [self.videoFilePath length] == 0) {
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;
    self.imagePicker.videoMaximumDuration = 10;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

    [self presentViewController:self.imagePicker animated:YES completion:nil];
}
}

}


<> P>当我使用仪器时,负责实例是核心基础。CFString。我不太熟悉在Objective-C中调试内存泄漏之类的东西。我希望能在这里帮忙!谢谢。

泄漏源于苹果的代码。唯一的错误是将UIImagePickerController保留为实例属性。停止那样做;每次显示UIImagePickerController时,将其创建为临时局部变量。除此之外,你也无能为力。

这个漏洞有多大?@matt 304 bytes现在做一点数学计算。在启动和退出你的应用程序之间,用户需要做多少次这样的操作,每次泄漏304字节,然后才能达到一个显著的量,比如说,1MB?@matt超过3k次。我知道这并不多,但我被告知内存泄漏是件坏事。你可以无视他们吗?你还有别的选择吗?漏洞来自苹果的代码。唯一的错误就是保留UIImagePickerController。停止那样做,继续前进。我会给出那个建议作为回答。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];



   UIImage *newImage = [self resizeImage:self.image toWidth:200 andHeight:200];

    self.image = newImage;


    if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
    }
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
    // A video was taken/selected!

    NSURL *videoUrl = info[UIImagePickerControllerMediaURL];
    self.movieUrl = videoUrl;

    self.videoFilePath = videoUrl.absoluteString;

    if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        // Save the video!

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum(self.videoFilePath, nil, nil, nil);
        }
    }
}

[self dismissViewControllerAnimated:YES completion:nil];