UIImagePickerController 是一個管理系統多媒體文件庫(相冊)中的圖片、視頻文件的視圖控制器,誕生於iOS4以前,雖然功能不是很完善,咱們仍能夠用這個視圖控制器作一些有創造性的開發,接下來會對其的常見和主要的使用逐個介紹。 php
首先 貼上一張圖,幫助咱們瞭解UIImagePickerController的整個構成,以及相關API被設計出來的用途: html
UIImagePickerController管理用戶在使用相機或者相簿時的交互,而且將交互產生的圖片、視頻文件傳送給其delegate對象。交互產生的文件類型是圖片仍是視頻取決於對使用其進行交互前對資源類型的設置,咱們能夠經過設置 UIImagePickerControllerSourceType的值來決定。詳情參考官方文檔的描述:git
A sourceType
of UIImagePickerControllerSourceTypeCamera
provides a user interface for taking a new picture or movie (on devices that support media capture).github
A sourceType
of UIImagePickerControllerSourceTypePhotoLibrary
or UIImagePickerControllerSourceTypeSavedPhotosAlbum
provides a user interface for choosing among saved pictures and movies.app
UIImagePickerControllerSourceType是一個枚舉選項,以下typedef enum UIImagePickerControllerSourceType : NSInteger {
UIImagePickerControllerSourceTypePhotoLibrary, UIImagePickerControllerSourceTypeCamera, UIImagePickerControllerSourceTypeSavedPhotosAlbum } UIImagePickerControllerSourceType;
對其相關描述以下:
UIImagePickerControllerSourceTypePhotoLibrary
Specifies the device’s photo library as the source for the image picker controller.ide
使用相冊 做爲 image picker controller的資源文件打開ui
UIImagePickerControllerSourceTypeCamera
Specifies the device’s built-in camera as the source for the image picker controller. Indicate the specific camera you want (front or rear, as available) by using the cameraDevice
property.url
使用攝像頭進行操做 獲取資源文件spa
UIImagePickerControllerSourceTypeSavedPhotosAlbum
Specifies the device’s Camera Roll album as the source for the image picker controller. If the device does not have a camera, specifies the Saved Photos album as the source設計
使用經過攝像頭處理獲得的資源文件做爲展現對象,若設備不支持攝像頭,則使用用戶經過其餘途徑獲取到的文件做爲展現對象。
在使用UIImagePickerController操做時,咱們不只要設置sourceType,還能夠設置allowsEdicting、allowImageEditing等屬性。
//UIImagePickerController常見用途
1. 調用攝像頭拍照
2. 從相冊中選擇
3. 從圖庫中選擇
UIImagePickerController 是系統提供的用來獲取圖片和視頻的接口;
用UIImagePickerController 類來獲取圖片視頻,大致分爲如下幾個步驟:
1. 初始化UIImagePickerController 類;
2. 設置UIImagePickerController 實例的數據來源類型(下面解釋);
3. 設置設置代理;
4. 若是須要作圖片修改的話設置allowsEditing =yes。
數據來源類型一共有三種:
enum { UIImagePickerControllerSourceTypePhotoLibrary ,//來自圖庫 UIImagePickerControllerSourceTypeCamera ,//來自相機 UIImagePickerControllerSourceTypeSavedPhotosAlbum //來自相冊 };
在用這些來源的時候最好檢測如下設備是否支持;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"支持相機"); } if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { NSLog(@"支持圖庫"); } if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { NSLog(@"支持相片庫"); }
調用攝像頭來獲取資源
- (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及其屬性 在須要獲取圖片的時候須要彈出窗口調用
[self presentViewController:picker animated:YES completion:nil];
咱們還須要代理來獲取咱們選中的圖片
UIImagePickerControllerDelegate
代理中一共三個方法 其中一個3.0 已經廢棄了,只剩下兩個咱們須要用的
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
當用戶選取完成後調用;
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
當用戶取消選取時調用;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
選取的信息都在info中,info 是一個字典。
字典中的鍵:
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 包含:
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 包含:
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
使用UIImagePickerController工做的步驟以下:
//
// ViewController.h
// 相機Demo
//
// Created by gzlx on 2018/9/18.
// Copyright © 2018年 VanZhang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// 相機Demo
//
// Created by gzlx on 2018/9/18.
// Copyright © 2018年 VanZhang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate>
@end
@implementation ViewController
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)takePictureButtonClick:(id)sender{
//檢查相機模式是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"sorry, no camera or camera is unavailable.");
return;
}
//得到相機模式下支持的媒體類型
NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
BOOL canTakePicture = NO;
for (NSString* mediaType in availableMediaTypes) {
if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
//支持拍照
canTakePicture = YES;
break;
}
}
//檢查是否支持拍照
if (!canTakePicture) {
NSLog(@"sorry, taking picture is not supported.");
return;
}
//建立圖像選取控制器
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
//設置圖像選取控制器的來源模式爲相機模式
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//設置圖像選取控制器的類型爲靜態圖像
imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil];
//容許用戶進行編輯
imagePickerController.allowsEditing = YES;
//設置委託對象
imagePickerController.delegate = self;
//以模視圖控制器的形式顯示
[self presentViewController:imagePickerController animated:YES completion:NULL];
}
- (IBAction)captureVideoButtonClick:(id)sender{
//檢查相機模式是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"sorry, no camera or camera is unavailable!!!");
return;
}
//得到相機模式下支持的媒體類型
NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
BOOL canTakeVideo = NO;
for (NSString* mediaType in availableMediaTypes) {
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
//支持攝像
canTakeVideo = YES;
break;
}
}
//檢查是否支持攝像
if (!canTakeVideo) {
NSLog(@"sorry, capturing video is not supported.!!!");
return;
}
//建立圖像選取控制器
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
//設置圖像選取控制器的來源模式爲相機模式
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//設置圖像選取控制器的類型爲動態圖像
imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeMovie, nil];
//設置攝像圖像品質
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
//設置最長攝像時間
imagePickerController.videoMaximumDuration = 30;
//容許用戶進行編輯
imagePickerController.allowsEditing = YES;
//設置委託對象
imagePickerController.delegate = self;
//以模式視圖控制器的形式顯示
[self presentViewController:imagePickerController animated:YES completion:NULL];
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
if (!error) {
NSLog(@"picture saved with no error.");
}
else{
NSLog(@"error occured while saving the picture%@", error);
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//打印出字典中的內容
NSLog(@"get the media info: %@", info);
//獲取媒體類型
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//判斷是靜態圖像仍是視頻
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//獲取用戶編輯以後的圖像
UIImage* editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
//將該圖像保存到媒體庫中
UIImageWriteToSavedPhotosAlbum(editedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
//獲取視頻文件的url
NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
//建立ALAssetsLibrary對象並將視頻保存到媒體庫
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error) {
if (!error) {
NSLog(@"captured video saved with no error.");
}else
{
NSLog(@"error occured while saving the video:%@", error);
}
}];
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:NULL];
}}
@end
最後附上本文的實戰demo 供參考 ,如果在運行中發生任何問題,均可以聯繫我.