我使用了一个很棒的教程(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];
}
问题:目前使用什么代替弃用方法?
答案 0 :(得分:34)
以下是如何使用新的图像选择器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;
}
答案 1 :(得分:7)
引用Apple文档:
imagePickerController:didFinishPickingImage:editingInfo:
告诉代表用户选择了一张图片。此方法是可选的。 (在iPhone OS 3.0中不推荐使用。 使用
imagePickerController:didFinishPickingMediaWithInfo:
而不是。)