1. 程式人生 > >iOS-系統 圖片、視頻 管理控制器UIImagePickerController

iOS-系統 圖片、視頻 管理控制器UIImagePickerController

duration 修改 sele 步驟 gif 用途 以及 接下來 創造性

   UIImagePickerController 是一個管理系統多媒體文件庫(相冊)中的圖片、視頻文件的視圖控制器,誕生於iOS4之前,雖然功能不是很完善,我們仍可以用這個視圖控制器做一些有創造性的開發,接下來會對其的常見和主要的使用逐個介紹。  

首先 貼上一張圖,幫助我們了解UIImagePickerController的整個構成,以及相關API被設計出來的用途:

技術分享圖片

  UIImagePickerController管理用戶在使用相機或者相簿時的交互,並且將交互產生的圖片、視頻文件傳送給其delegate對象。交互產生的文件類型是圖片還是視頻取決於對使用其進行交互前對資源類型的設置,我們可以通過設置 UIImagePickerControllerSourceType的值來決定。詳情參考官方文檔的描述:

  • A sourceType of UIImagePickerControllerSourceTypeCameraprovides a user interface for taking a new picture or movie (on devices that support media capture).

  • A sourceType of UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeSavedPhotosAlbum provides a user interface for choosing among saved pictures and movies.

UIImagePickerControllerSourceType是一個枚舉選項,如下typedef enum UIImagePickerControllerSourceType : NSInteger {
    UIImagePickerControllerSourceTypePhotoLibrary, 
    UIImagePickerControllerSourceTypeCamera,
    UIImagePickerControllerSourceTypeSavedPhotosAlbum
} UIImagePickerControllerSourceType;

對其相關描述如下:
UIImagePickerControllerSourceTypePhotoLibrary

Specifies the device’s photo library as the source for the image picker controller.

使用相冊 作為 image picker controller的資源文件打開

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.

使用攝像頭進行操作 獲取資源文件

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工作的步驟如下:

1.檢查媒體來源模式是否可用

2.檢查該來源模式下所支持的媒體類型

3.創建圖像選取控制器,設置其屬性並顯示

4.在委托協議方法中處理交互結果得到的資源 現在馬上進入實戰進行體驗一把: 在一個控制器上添加如下代碼
- (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 stringWithCString:kUTTypeImage encoding:NSUTF8StringEncoding]]) {
            //支持拍照
            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 presentModalViewController:imagePickerController animated:YES];
}

- (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 *)kUTTypeImage]) {
            //支持攝像
            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 presentModalViewController:imagePickerController animated:YES];
    
}

- (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 *)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 dismissModalViewControllerAnimated:YES];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}


 

iOS-系統 圖片、視頻 管理控制器UIImagePickerController