2009-08-04 132 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

相关问题