2013-01-14 138 views
1

我目前有一个UIImageView和一个UIImage。我如何允许用户从相机胶卷中选择他们的照片之一?从iPhone相机胶卷获取图像

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

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

+2

为'类参考UIImagePickerViewController'是你最好的朋友。 – 2013-01-14 22:25:57

回答

1

使用UIImagePickerController,请参阅Apple文档。

选择源类型:

@property (nonatomic) UIImagePickerControllerSourceType sourceType 

从上这些:

UIImagePickerControllerSourceTypePhotoLibrary, 
UIImagePickerControllerSourceTypeCamera, 
UIImagePickerControllerSourceTypeSavedPhotosAlbum 
3

试试这个代码

它是帮助你从cameraroll & photolibrary得到的图像。使用UIImagePickerViewController

,如果你把图片

  1. 从相机(在开放作为的UINavigationController)。
  2. 从图库(对于作为UIpopovercontroller打开的ipad &用于作为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 ........ 
} 
相关问题