选择照片后UIPickerController内存泄漏

时间:2018-02-13 15:27:20

标签: ios objective-c memory-leaks uiimagepickercontroller

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

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

}

当我使用乐器时,负责的实例在Core Foundation中。 CFString字符串。我不太熟悉在Objective-C中调试内存泄漏。我很感激这里的一只手!谢谢。

1 个答案:

答案 0 :(得分:1)

漏洞来自Apple的代码。 唯一错误的是保留UIImagePickerController作为实例属性。别那样做;每次呈现时,都会将UIImagePickerController创建为临时局部变量。除此之外,你无能为力。