UIImagePickerController


iOS 獲取圖片有三種方法:

1. 直接調用攝像頭拍照

2. 從相冊中選擇

3. 從圖庫中選擇

UIImagePickerController 是系統提供的用來獲取圖片和視頻的接口;

用UIImagePickerController 類來獲取圖片視頻,大體分為以下幾個步驟:

1. 初始化UIImagePickerController 類;

2. 設置UIImagePickerController 實例的數據來源類型(下面解釋);

3. 設置設置代理;

4. 如果需要做圖片修改的話設置allowsEditing =yes。

數據來源類型一共有三種:

1
2
3
4
5
enum {
    UIImagePickerControllerSourceTypePhotoLibrary , //來自圖庫
    UIImagePickerControllerSourceTypeCamera , //來自相機
    UIImagePickerControllerSourceTypeSavedPhotosAlbum  //來自相冊
};

在用這些來源的時候最好檢測以下設備是否支持;

1
2
3
4
5
6
7
8
9
10
11
12
  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
     {
         NSLog(@ "支持相機" );
     }
     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
     {
         NSLog(@ "支持圖庫" );
     }
     if  ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
     {
         NSLog(@ "支持相片庫" );
     }

調用攝像頭來獲取資源

1
2
3
4
5
6
7
8
9
- (void)viewDidLoad {
     [ super  viewDidLoad];
     picker = [[UIImagePickerController alloc]init];
     picker.view.backgroundColor = [UIColor orangeColor];
     UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
     picker.sourceType = sourcheType;
     picker.delegate = self;
     picker.allowsEditing = YES;
}

上面只是實例了UIImagePickerController及其屬性 在需要獲取圖片的時候需要彈出窗口調用

1
[self presentViewController:picker animated:YES completion:nil];

我們還需要代理來獲取我們選中的圖片

1
UIImagePickerControllerDelegate

代理中一共三個方法 其中一個3.0 已經廢棄了,只剩下兩個我們需要用的

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

當用戶選取完成后調用;

1
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;

當用戶取消選取時調用;

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

選取的信息都在info中,info 是一個字典。

字典中的鍵:

1
2
3
4
5
6
7
NSString *const  UIImagePickerControllerMediaType ;指定用戶選擇的媒體類型(文章最后進行擴展)
NSString *const  UIImagePickerControllerOriginalImage ;原始圖片
NSString *const  UIImagePickerControllerEditedImage ;修改后的圖片
NSString *const  UIImagePickerControllerCropRect ;裁剪尺寸
NSString *const  UIImagePickerControllerMediaURL ;媒體的URL
NSString *const  UIImagePickerControllerReferenceURL ;原件的URL
NSString *const  UIImagePickerControllerMediaMetadata;當來數據來源是照相機的時候這個值才有效

UIImagePickerController 的更多參數參考這里

代理中的功能參考這里

UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie

KUTTypeImage 包含:

1
2
3
4
5
6
7
8
9
10
11
const CFStringRef  kUTTypeImage ;抽象的圖片類型
const CFStringRef  kUTTypeJPEG ;
const CFStringRef  kUTTypeJPEG2000 ;
const CFStringRef  kUTTypeTIFF ;
const CFStringRef  kUTTypePICT ;
const CFStringRef  kUTTypeGIF ;
const CFStringRef  kUTTypePNG ;
const CFStringRef  kUTTypeQuickTimeImage ;
const CFStringRef  kUTTypeAppleICNS 
const CFStringRef kUTTypeBMP;
const CFStringRef  kUTTypeICO;

KUTTypeMovie 包含:

1
2
3
4
5
6
7
8
9
10
const CFStringRef  kUTTypeAudiovisualContent ;抽象的聲音視頻
const CFStringRef  kUTTypeMovie ;抽象的媒體格式(聲音和視頻)
const CFStringRef  kUTTypeVideo ;只有視頻沒有聲音
const CFStringRef  kUTTypeAudio ;只有聲音沒有視頻
const CFStringRef  kUTTypeQuickTimeMovie ;
const CFStringRef  kUTTypeMPEG ;
const CFStringRef  kUTTypeMPEG4 ;
const CFStringRef  kUTTypeMP3 ;
const CFStringRef  kUTTypeMPEG4Audio ;
const CFStringRef  kUTTypeAppleProtectedMPEG4Audio;
 
 

轉載自:http://blog.csdn.net/kingsley_cxz/article/details/9157093

 

1.UIImagePickerController的靜態方法:

    imagepicker = [[UIImagePickerController alloc]init];
    //UIImagePickerController靜態方法判斷設備是否支持照相機/圖片庫/相冊功能
    /*
     typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
     UIImagePickerControllerSourceTypePhotoLibrary,
     UIImagePickerControllerSourceTypeCamera,
     UIImagePickerControllerSourceTypeSavedPhotosAlbum
     };
     */
    BOOL isCameraSupport = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    NSLog(@"support camera:%d",isCameraSupport);
    
    //UIImagePickerController靜態方法判斷設備是否支持前置攝像頭/后置攝像頭
    /*
     typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
     UIImagePickerControllerCameraDeviceRear,
     UIImagePickerControllerCameraDeviceFront
     };
     */
    BOOL isRearSupport = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    NSLog(@"rear support:%d",isRearSupport);
    
    //UIImagePickerController靜態方法判斷設備是否支持前置攝像頭閃光燈/后置攝像頭閃光燈
    /*
     typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
     UIImagePickerControllerCameraDeviceRear,
     UIImagePickerControllerCameraDeviceFront
     };
     */
    BOOL isFlushSupport = [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear];
    NSLog(@"rear flash support:%d",isFlushSupport);
    
    //UIImagePickerController靜態方法返回前置攝像頭/后置攝像頭支持的拍攝類型
    /*
     typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
     UIImagePickerControllerCameraDeviceRear,
     UIImagePickerControllerCameraDeviceFront
     };
     */
    //返回類型有照片和視頻
    /*
     enum {
     UIImagePickerControllerCameraCaptureModePhoto,
     UIImagePickerControllerCameraCaptureModeVideo
     };
     typedef NSUInteger UIImagePickerControllerCameraCaptureMode;
     */
    NSArray* captureModes = [UIImagePickerController availableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceRear];
    for (NSNumber* mode in captureModes) {
        NSLog(@"capture modes:%d",[mode integerValue]);
    }
    
    //UIImagePickerController靜態方法返回照相機/相冊/照片庫所支持的媒體類型
    /*
     typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
     UIImagePickerControllerSourceTypePhotoLibrary,
     UIImagePickerControllerSourceTypeCamera,
     UIImagePickerControllerSourceTypeSavedPhotosAlbum
     };
     */
    //返回類型有kUTTypeMovie,kUTTypeImage,其他類型均在<MobileCoreServices/MobileCoreServices.h>下
    NSArray* mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    for (NSString* type in mediaTypes) {
        NSLog(@"media types:%@",type);
    }

2.UIImagePickerController屬性詳解:

    //指定使用照相機模式,可以指定使用相冊/照片庫
    imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    //設置當拍照完或在相冊選完照片后,是否跳到編輯模式進行圖片剪裁。只有當showsCameraControls屬性為true時才有效果
    imagepicker.allowsEditing = YES;
    //設置拍照時的下方的工具欄是否顯示,如果需要自定義拍攝界面,則可把該工具欄隱藏
    imagepicker.showsCameraControls  = YES;
    //設置使用后置攝像頭,可以使用前置攝像頭
    imagepicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    //設置閃光燈模式
    /*
     typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
     UIImagePickerControllerCameraFlashModeOff  = -1,
     UIImagePickerControllerCameraFlashModeAuto = 0,
     UIImagePickerControllerCameraFlashModeOn   = 1
     };
     */
    imagepicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
    //設置相機支持的類型,拍照和錄像
    imagepicker.mediaTypes = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeMovie];
    //設置拍攝時屏幕的view的transform屬性,可以實現旋轉,縮放功能
   // imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI*45/180);
   // imagepicker.cameraViewTransform = CGAffineTransformMakeScale(1.5, 1.5);
    //所有含有cameraXXX的屬性都必須要sourceType是UIImagePickerControllerSourceTypeCamera時設置才有效果,否則會有異常
    //設置UIImagePickerController的代理
    imagepicker.delegate = self;

-(void)showCamera{
    //用模態方式呈現imagepicker
    [self presentModalViewController:imagepicker animated:YES];
}

使用 imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI*45/180);旋轉45度的效果:

使用imagepicker.cameraViewTransform = CGAffineTransformMakeScale(1.5, 1.5);全屏的效果,同時imagepicker.showsCameraControls  =NO;隱藏工具欄:

使用imagepicker.allowsEditing = YES;出現的圖片編輯效果,只有imagepicker.showsCameraControls  = YES;才有效果:

使用 imagepicker.mediaTypes = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeMovie];支持拍照和視頻的前后對比效果:

3.UIImagePickerController回調詳解:

//成功獲得相片還是視頻后的回調
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //通過UIImagePickerControllerMediaType判斷返回的是照片還是視頻
    NSString* type = [info objectForKey:UIImagePickerControllerMediaType];
    //如果返回的type等於kUTTypeImage,代表返回的是照片,並且需要判斷當前相機使用的sourcetype是拍照還是相冊
    if ([type isEqualToString:(NSString*)kUTTypeImage]&&picker.sourceType==UIImagePickerControllerSourceTypeCamera) {
        //獲取照片的原圖
        UIImage* original = [info objectForKey:UIImagePickerControllerOriginalImage];
        //獲取圖片裁剪的圖
        UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage];
        //獲取圖片裁剪后,剩下的圖
        UIImage* crop = [info objectForKey:UIImagePickerControllerCropRect];
        //獲取圖片的url
        NSURL* url = [info objectForKey:UIImagePickerControllerMediaURL];
        //獲取圖片的metadata數據信息
        NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
        //如果是拍照的照片,則需要手動保存到本地,系統不會自動保存拍照成功后的照片
        UIImageWriteToSavedPhotosAlbum(edit, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }else{
    
    }
    //模態方式退出uiimagepickercontroller
    [imagepicker dismissModalViewControllerAnimated:YES];
}
//取消照相機的回調
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //模態方式退出uiimagepickercontroller
    [imagepicker dismissModalViewControllerAnimated:YES];
}
//保存照片成功后的回調
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
  contextInfo:(void *)contextInfo{
    
    NSLog(@"saved..");
}

4.自定義相機拍照畫面:

   //設置拍照時的下方的工具欄是否顯示,如果需要自定義拍攝界面,則可把該工具欄隱藏
    imagepicker.showsCameraControls  = NO;

    UIToolbar* tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-55, self.view.frame.size.width, 75)];
    tool.barStyle = UIBarStyleBlackTranslucent;
    UIBarButtonItem* cancel = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelCamera)];
    UIBarButtonItem* add = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePhoto)];
    [tool setItems:[NSArray arrayWithObjects:cancel,add, nil]];
   //把自定義的view設置到imagepickercontroller的overlay屬性中

   imagepicker.cameraOverlayView = tool;

-(void)cancelCamera{
    [imagepicker dismissModalViewControllerAnimated:YES];
}
-(void)savePhoto{
    //拍照,會自動回調- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info,對於自定義照相機界面,拍照后回調可以不退出實現連續拍照效果
    [imagepicker takePicture];
}

 

5.UIImagePickerController是繼承UINavigationController,所以可以push和pop一些viewcontroller進行導航效果。例如,自定義照相機畫面的時候可以在拍攝完后push一個viewcontroller用於對照片進行編輯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM