在应用程序中获取相机拾取的最近图像

时间:2012-08-07 05:50:41

标签: iphone objective-c ios

我正在开发一个使用相机的应用程序。我正在使用浏览选项浏览图库中的照片。如果需要,我也会提供拍照的选项。为了在浏览时获取所选照片,我实现了UIImagePickerControllerDelegate方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
 {
      [self dismissModalViewControllerAnimated:YES];
      imageView.image = img;    
  }

我在此方法中获取所选图像。但是如何在我的imageView中获取相机捕获的图像而不将其保存到图库中?我会用同样的方法获得图像吗?

3 个答案:

答案 0 :(得分:2)

您正在使用imagePickerController:didFinishPickingImage:editingInfo:。这在ios 3.0中已弃用。请参阅UIImagePickerControllerDelegate并参考此link为什么不使用弃用的方法。

您必须使用imagePickerController:didFinishPickingMediaWithInfo:,如下所示:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
     yourImageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
     [self dismissModalViewControllerAnimated:YES];
}

这适用于您的相机和照片库。您也不需要保存使用相机拍摄的图像。您可以像此代码一样直接将其分配给图像视图。

希望这会对你有所帮助。

答案 1 :(得分:1)

这很简单,您只需要将UIImage直接传递到UIImageView

 yourImageView.image = img;

答案 2 :(得分:0)

请试试这个:

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"Media Info: %@", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
    UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //Save Photo to library only if it wasnt already saved i.e. its just been taken
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        yourImageView.image = img }
}
[self dismissModalViewControllerAnimated:YES];
}