Max
Max

Reputation: 173

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. CFString. I am not very familiar with debugging memory leak's like these in Objective-C. I'd appreciate a hand here! Thank You.

Upvotes: 0

Views: 230

Answers (1)

matt
matt

Reputation: 534914

The leak is coming from Apple's code. The only thing you are doing wrong is retaining the UIImagePickerController as an instance property. Stop doing that; create the UIImagePickerController as a temporary local variable each time you present it. Apart from that, there's nothing you can do.

Upvotes: 1

Related Questions