2011-06-06 366 views
2

我對此有點困惑,因爲我已經看到太多不同的變體,並且不確定哪一個是正確的方式。目前,我有:UIImagePickerController的正確發佈

- (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.image = self.fitImage; 

    [self dismissModalViewControllerAnimated:YES]; 

    [picker release]; 
} 

我在第一種方法分配UIImagePickerController,但不會是合乎邏輯的,只在第2個方法,當我關閉它釋放?

回答

6

不,因爲它通過presentModelViewController以模態方式呈現時保留。這是您在展示新視圖控制器時會發現的常見模式,無論是否使用模態,自定義視圖控制器。這很好:

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

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

    [self presentModalViewController:picker animated:YES]; 

    [picker release]; 
} 
+0

很感謝。這意味着我不需要發佈在didFinishPickingMediaWithInfo中:) – 2011-06-06 12:24:04