开发者

Proper release of UIImagePickerController

开发者 https://www.devze.com 2023-03-10 11:49 出处:网络
I\'m a bit confused with this as I\'ve seen way too many different variants and not sure which one is the correct way. Currently I have:

I'm a bit confused with this as I've seen way too many different variants and not sure which one is the correct way. Currently I have:

- (IBAction)pickImageFromLibrary:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentModalViewController:picker animated:YES];

    //  [picker release];
}

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 10.0f, 320.0f, 264.0f)];

    self.studyView = imageView;

    [imageView release];

    [self.tableView setTableHeaderView:studyView];

    self.fitImage = [ImageHelper image:image fitInView:studyView];

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }

    studyView.i开发者_JAVA百科mage = self.fitImage;

    [self dismissModalViewControllerAnimated:YES];

    [picker release];
 }

I'm allocating the UIImagePickerController in the first method but wouldn't it be logical to only release it in the 2nd method when I dismiss it?


No, because it's retained when presented modally, via presentModelViewController. This is the common pattern you'll find when presenting new view controllers, whether modally, custom view controllers or not. This is fine:

- (IBAction)pickImageFromLibrary:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentModalViewController:picker animated:YES];

    [picker release];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消