从 iPhone 相机胶卷中获取图像

Posted

技术标签:

【中文标题】从 iPhone 相机胶卷中获取图像【英文标题】:Getting an image from the iPhone camera roll 【发布时间】:2013-01-14 22:23:54 【问题描述】:

我目前有一个UIImageView 和一个UIImage。如何允许用户从相机胶卷中选择一张照片?

有possibleduplicates,但我没有运气。这可能是因为我没有在 Interface Builder 中连接。

我想要像UIImage * image = [UIImage imageFromCameraRoll] 这样简单的东西,但不幸的是,这是不可能的(至少我不这么认为)。

【问题讨论】:

UIImagePickerViewController 的类参考是你最好的朋友。 【参考方案1】:

试试这个代码

帮助您从camerarollphotolibrary 获取图像。使用UIImagePickerViewController

如果你拍照

    来自相机(在两个都打开为 UINavigationcontroller)。 来自 Gallery(对于 ipad 作为 UIpopovercontroller 打开和对于 iphone 作为 nvigationcontroller 打开)。

第一组委托

@interface camera : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIPopoverControllerDelegate>

从相机获取图像

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        
            UIImagePickerController *imagePicker =
            [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType =
            UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                      (NSString *) kUTTypeImage,
                                      nil];

            imagePicker.allowsEditing = YES;

            imagePicker.wantsFullScreenLayout = YES;

            [self presentViewController:imagePicker animated:YES completion:nil];
            newMedia = YES;
            iscamera = 0;
        
        else
        
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error to access Camera"
                                                            message:@""
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];

        

从图库中获取图片

 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    
        UIImagePickerController *_picker=nil;
        if (popoverController) 
            [popoverController dismissPopoverAnimated:NO];

        
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;        
        _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        _picker.wantsFullScreenLayout = YES;

        //[popoverController presentPopoverFromBarButtonItem:sender
                               //   permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 

           [self presentViewController:_picker animated:YES completion:nil];


         else
        
            popoverController = [[UIPopoverController alloc] initWithContentViewController:_picker];
            [popoverController setDelegate:self];
            [popoverController presentPopoverFromRect:btn.frame
                                               inView:self.view
                             permittedArrowDirections:UIPopoverArrowDirectionLeft
                                             animated:YES];
        
    
    else
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error access photo library"
                                                        message:@"your device non support photo library"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];

    

您在委托方法中得到的两个结果

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

   // write your code here ........
 

【讨论】:

【参考方案2】:

使用 UIImagePickerController,请参阅 Apple 文档。

选择来源类型:

@property (nonatomic) UIImagePickerControllerSourceType sourceType

从这些开始:

UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum

【讨论】:

以上是关于从 iPhone 相机胶卷中获取图像的主要内容,如果未能解决你的问题,请参考以下文章

检测 iPhone 相机方向

保存用相机拍摄或从相机胶卷中选择的图像时出现长时间延迟 - iPhone

除了相机胶卷,如何从 iPhone 中获取所有资产?

自定义相册中的图像保存问题以及 iphone 中的默认相机胶卷

iOS:使用缩放模拟 iPhone 相机胶卷

如何对相机胶卷中的照片进行方形切割?