IOS獲取圖片有三種方式:
- UIImagePickerControllerSourceTypePhotoLibrary 相冊(cè)
- UIImagePickerControllerSourceTypeCamera 相機(jī)
- UIImagePickerControllerSourceTypeSavedPhotosAlbum 圖庫(kù)
最好先判斷設(shè)備數(shù)據(jù)來源是否支持
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
}
步驟:
第一步:選擇獲取圖片的方式
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"從相冊(cè)選取", nil];
[sheet showInView:self.view];
第二部:創(chuàng)建UIImagePickerController
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSUInteger sourceType = UIImagePickerControllerSourceTypeCamera;
switch (buttonIndex) {
case 0: //相機(jī)
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1: //相冊(cè)
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
case 2: //取消
return;
break;
default:
break;
}
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = sourceType; //圖片來源
imagePicker.showsCameraControls? = YES;// 設(shè)置是否顯示相機(jī)原生 UI 默認(rèn)yes
imagePicker.allowsEditing = YES; //允許剪切圖片
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront; //前置攝像頭
[self presentViewController:imagePicker animated:YES completion:nil];
}
第三步:兩個(gè)delegate方法?
確定使用相片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];//使用剪切后的圖片
self.authenPhotoView.imageView.image = image;
}
取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
知識(shí)補(bǔ)充:
1 - didFinishPickingMediaWithInfo 中的 info 可以選擇使用的圖片:
UIImagePickerControllerMediaType ?-- ?指定用戶選擇的媒體類型(文章最后進(jìn)行擴(kuò)展)
UIImagePickerControllerOriginalImage -- 原始圖片
UIImagePickerControllerEditedImage -- 修改后的圖片
UIImagePickerControllerCropRect -- 裁剪尺寸
UIImagePickerControllerMediaURL -- 媒體的URL
UIImagePickerControllerReferenceURL -- 原件的URL
UIImagePickerControllerMediaMetadata;當(dāng)來數(shù)據(jù)來源是照相機(jī)的時(shí)候這個(gè)值才有效
UIImage *image = [info objectForKey: UIImagePickerControllerEditedImage ];
2 -? 攝像頭 -> cameraDevice
UIImagePickerControllerCameraDeviceRear -- 后置攝像頭
UIImagePickerControllerCameraDeviceFront -- 前置攝像頭
可以判斷攝像頭是否支持
if ([UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceRear]) {
}
3 - 設(shè)置相機(jī)的兩種模式 -> cameraCaptureMode
UIImagePickerControllerCameraCaptureModePhoto -- 照相模式幌墓,默認(rèn)
UIImagePickerControllerCameraCaptureModeVideo -- 錄像模式
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
4 - 媒體類型 -> mediaTypes
需要 #import才能用 kUTTypeImage 和 KUTTypeMovie 。
UIImagePickerControllerMediaType 包含著KUTTypeImage 和KUTTypeMovie ;默認(rèn)kUTTypeImage块差;
imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie];
4.1 - kUTTypeImage 包含:? ? ? ? ? ?
?kUTTypeImage -- 抽象的圖片類型? ? ? ? ?
? kUTTypeJPEG ? ? ? ? ?
kUTTypeJPEG2000 ? ? ? ? ?
kUTTypeTIFF ? ? ? ??
?kUTTypePICT ? ? ? ? ??
?kUTTypeGIF ? ? ? ?
?kUTTypePNG ? ? ? ??
?kUTTypeQuickTimeImage ? ? ? ? ?
kUTTypeAppleICNS ? ? ??
kUTTypeBMP ? ? ?
kUTTypeICO? ? ? ??
4.2 - kUTTypeMovie 包含: ? ? ? ? ? ?
kUTTypeAudiovisualContent -- 抽象的聲音視頻? ? ? ? ? ?
?kUTTypeMovie -- 抽象的媒體格式(聲音和視頻) ? ? ? ? ? ?
kUTTypeVideo -- 只有視頻沒有聲音? ? ? ? ? ?
?kUTTypeAudio -- 只有聲音沒有視頻? ? ? ? ? ??
kUTTypeQuickTimeMovie ? ? ? ? ??
kUTTypeMPEG ? ? ? ??
?kUTTypeMPEG4 ? ? ? ? ?
kUTTypeMP3 ? ? ? ??
?kUTTypeMPEG4Audio ? ? ? ??
kUTTypeAppleProtectedMPEG4Audio ?
5 - 設(shè)置閃光燈模式 -> cameraFlashMode
UIImagePickerControllerCameraFlashModeOff? = -1 ?// 關(guān)
UIImagePickerControllerCameraFlashModeAuto = 0 ? // 自動(dòng)馆蠕,默認(rèn)
UIImagePickerControllerCameraFlashModeOn? = 1? ? // 開
imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
6 - 設(shè)置錄像質(zhì)量 -> videoQuality
UIImagePickerControllerQualityTypeHigh = 0 ? ? // 高質(zhì)量刽锤,highest quality
UIImagePickerControllerQualityTypeMedium = 1 ? // 中質(zhì)量彪蓬,默認(rèn),Wi-Fi
UIImagePickerControllerQualityTypeLow = 2 ? ?// 低質(zhì)量悲立,cellular network
UIImagePickerControllerQualityType640x480 = 3 ? // VGA 質(zhì)量鹿寨,VGA quality
UIImagePickerControllerQualityTypeIFrame1280x720 = 4?
UIImagePickerControllerQualityTypeIFrame960x540 = 5
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
7 - 錄像
[imagePickerController startVideoCapture]; ? // 開始錄像
[imagePickerController stopVideoCapture]; ? // 停止錄像
imagePickerController.videoMaximumDuration = 600; ? // 設(shè)置錄像時(shí)間長(zhǎng)度 default is 10 minutes. apply only mediaTypes includes kUTTypeMovie
8 - 自定義相機(jī)視圖
imagePickerController.showsCameraControls = YES; ? //設(shè)置是否顯示相機(jī)原生 UI ;默認(rèn)使用薪夕,為no時(shí)才可以自定義相機(jī)視圖
imagePickerController.cameraOverlayView = myView; ? ? // 自定義相機(jī)視圖
imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(0.25 * M_PI); ? //改變相機(jī)視圖 - 旋轉(zhuǎn) 45 度脚草,需要輸入的參數(shù)為弧度,45/180 * M_PI原献,1 度 = PI/180 弧度;
參考:http://www.cnblogs.com/QianChia/p/5758377.html