【问题标题】:What to use in place of a deprecated UIImagePickerControllerDelegate method?用什么代替已弃用的 UIImagePickerControllerDelegate 方法?
【发布时间】:2009-08-04 01:04:20
【问题描述】:

我遵循了一个很棒的教程 (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];


}

问题:目前使用什么来代替已弃用的方法?

【问题讨论】:

    标签: iphone iphone-sdk-3.0 uiimagepickercontroller deprecated


    【解决方案1】:

    以下是如何使用新的图像选择器 API 的简要说明。

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

    @interface MyClass : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
    UIImagePickerController*    imagePicker;
    }
    @property(nonatomic,retain) UIImagePickerController* imagePicker;
    
    - (IBAction) takePicture:(id)sender;
    
    @end
    

    调出图像选择器的方法是这样的。它在这里声明为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;
    }
    

    【讨论】:

    • 嘿,谢谢。我不是为了积分在这里闲逛。这是为了好的因果报应,你只是把它提高了一个档次:-)
    • allowImageEditing 不是也被弃用了吗?
    • 我相信它在 OS 3.0 中仍然可用。也许您正在考虑一个尚未发布的操作系统 ;-)
    • 很好的答案@Ramin。还可以提到 kUTTypeImage 常量需要链接 MobileCoreServices.framework。
    【解决方案2】:

    引用 Apple 文档:

    imagePickerController:didFinishPickingImage:editingInfo:
    

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

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 2022-09-23
      • 2016-08-16
      • 2023-03-13
      相关资源
      最近更新 更多