代替过时UIImagePickerControllerDelegate方法的使用呢?
-
22-07-2019 - |
题
我跟着一个伟大的教程( 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];
}
问题:<?强>什么代替弃用方法的目前使用的强>
解决方案
引述苹果文档:
imagePickerController:didFinishPickingImage:editingInfo:
告诉用户拾取的图像委托。这种方法是可选的。 (的弃用在iPhone OS 3.0 强> 使用
imagePickerController:didFinishPickingMediaWithInfo:
不是的。)
其他提示
下面介绍如何使用新的图像拾取API一言以蔽之。
首先,你需要声明的是这样一类,因为它的设置本身作为图像拾取委托:
@interface MyClass : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
UIImagePickerController* imagePicker;
}
@property(nonatomic,retain) UIImagePickerController* imagePicker;
- (IBAction) takePicture:(id)sender;
@end
这带来了影像选择器会去像这样的方法。它宣称这里在Interface Builder的IBAction
这样你就可以将其直接连线到控制(如按钮)。它还检查,这样如果你在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;
}
不隶属于 StackOverflow