简体   繁体   English

选择照片后UIPickerController内存泄漏

[英]UIPickerController memory leak after selecting photo

I am having a memory leak whenever I select a photo within my image picker controller. 每当在图像选择器控制器中选择一张照片时,我就会发生内存泄漏。 I have tried doing my own research as to why this happens but I have found nothing close to my situation. 我曾尝试研究这种情况的发生原因,但没有发现与我的情况相近的信息。 Various tutorials I have learnt from have a memory leak when using the image picker controller as well. 我从中学到的各种教程在使用图像选择器控制器时也会发生内存泄漏。 All I am doing is choosing a photo and then the image picker controller dismisses. 我要做的就是选择一张照片,然后图像选择器控制器关闭。 I have tried finding a solution for many days now, but no luck, I tend to try to find solutions before asking for help, any help is greatly appreciated, here is my code. 我已经尝试寻找解决方案很多天了,但是没有运气,我倾向于在寻求帮助之前尝试找到解决方案,非常感谢您的帮助,这是我的代码。

@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];
}

} }

- (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];

} }

When I use instruments I get, that the responsible instance is in Core Foundation. 当我使用工具时,负责的实例在Core Foundation中。 CFString. CFString字符串。 I am not very familiar with debugging memory leak's like these in Objective-C. 我对调试Objective-C中的内存泄漏不太熟悉。 I'd appreciate a hand here! 我在这里很感激! Thank You. 谢谢。

The leak is coming from Apple's code. 泄漏来自Apple的代码。 The only thing you are doing wrong is retaining the UIImagePickerController as an instance property. 唯一做错的是将UIImagePickerController保留为实例属性。 Stop doing that; 别那样做; create the UIImagePickerController as a temporary local variable each time you present it. 每次显示时,都将UIImagePickerController创建为临时局部变量。 Apart from that, there's nothing you can do. 除此之外,您无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM