3

我遵循了一个很棒的教程 ( http://iphone.zcentric.com/2008/08/28/using-a-uiimagepickercontroller/ ) 使用 UIImagePickerController 从 iPhone 上的相册或相机中获取图像。问题是,该教程有点过时了,并且文档引用了代理使用的一种方法,因为自 3.0 以来该方法已被贬值。问题是,文档未能提供有关使用什么的线索?不推荐使用的方法是:

– imagePickerController:didFinishPickingImage:editingInfo:

上述方法使用如下:

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

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


}

问题:目前使用什么来代替不推荐使用的方法?

4

2 回答 2

37

简而言之,以下是如何使用新的图像选择器 API。

首先,您需要一个像这样声明的类,因为它将自己设置为图像选择器委托:

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

- (IBAction) takePicture:(id)sender;

@end

调出图像选择器的方法是这样的。它在这里被声明为 an,IBAction因此您可以直接将其连接到 Interface Builder 中的控件(如按钮)。它还会检查,如果你在 iPhone 上,它会调出相机界面,但在 iPod Touch 上,它会调出图库选择器:

#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;
}
于 2009-08-04T17:24:55.170 回答
7

引用苹果文档:

imagePickerController:didFinishPickingImage:editingInfo:

告诉代理用户选择了一张图片。此方法是可选的。(在 iPhone OS 3.0 中已弃用。 改用imagePickerController:didFinishPickingMediaWithInfo:

于 2009-08-04T01:37:32.840 回答