2009-08-04 101 views
2

我遵循一個很好的教程(http://iphone.zcentric.com/2008/08/28/using-a-uiimagepickercontroller/)關於使用UIImagePickerController從iPhone上的相冊或相機獲取圖像。問題是,本教程有點過時,文檔引用了自3.0開始折舊的委託使用的方法。問題在於,文檔未能提供有關如何使用的線索?不推薦使用的方法是:用用什麼來代替已棄用的UIImagePickerControllerDelegate方法?

– imagePickerController:didFinishPickingImage:editingInfo: 

以上方法如下:

- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)img editingInfo:(NSDictionary*)editInfo 
{ 

    image.image = img; 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 


} 

問題:什麼是目前實行的方法已過時的使用呢?

回答

7

引述蘋果文檔:

imagePickerController:didFinishPickingImage:editingInfo: 

告訴用戶選擇了一個形象的代表。此方法是可選的。 (不贊成使用,iPhone OS 3.0。使用imagePickerController:didFinishPickingMediaWithInfo:代替

34

下面介紹如何使用新的圖像拾取API一言以蔽之。

首先,你需要聲明的是這樣,因爲它的設置本身作爲圖像拾取委託類:

@interface MyClass : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> { 
UIImagePickerController* imagePicker; 
} 
@property(nonatomic,retain) UIImagePickerController* imagePicker; 

- (IBAction) takePicture:(id)sender; 

@end 

,帶來了影像選擇器會去像這樣的方法。它在這裏聲明爲IBAction,所以你可以直接將它連接到Interface Builder中的控件(如按鈕)。它還檢查,這樣如果你在iPhone上它帶來了相機接口,但在iPod觸摸它帶來了畫廊選擇器:

#import <MobileCoreServices/UTCoreTypes.h> 
... 
@synthesize imagePicker = _imagePicker; 
... 

- (void) takePicture:(id)sender 
{ 
     if (!_imagePicker) { 
      self.imagePicker = [[UIImagePickerController alloc] init]; 
      self.imagePicker.delegate = self; 
     } 

     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
      self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
      NSArray* mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; 
      self.imagePicker.mediaTypes = mediaTypes; 
     } else { 
      self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
      self.imagePicker.allowsImageEditing = YES; 
     } 

    [self presentModalViewController:self.imagePicker animated:YES]; 
} 

然後,你需要這兩種方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 

    // MediaType can be kUTTypeImage or kUTTypeMovie. If it's a movie then you 
    // can get the URL to the actual file itself. This example only looks for images. 
    // 
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    // NSString* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL]; 

    // Try getting the edited image first. If it doesn't exist then you get the 
    // original image. 
    // 
    if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {  
     UIImage* picture = [info objectForKey:UIImagePickerControllerEditedImage]; 
     if (!picture) 
      picture = [info objectForKey:UIImagePickerControllerOriginalImage];  

      // **You can now do something with the picture. 
    } 
    self.imagePicker = nil; 
} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
    self.imagePicker = nil; 
} 
+1

你應該接受答案。 – mk12 2009-08-18 16:13:25

相關問題