gpt4 book ai didi

iphone - 使用什么来代替已弃用的 UIImagePickerControllerDelegate 方法?

转载 作者:行者123 更新时间:2023-12-03 19:26:11 25 4
gpt4 key购买 nike

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

– imagePickerController:didFinishPickingImage:editingInfo:

上述方法的使用方法如下:

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

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


}

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

最佳答案

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

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

@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;
}

关于iphone - 使用什么来代替已弃用的 UIImagePickerControllerDelegate 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1225323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com