您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 中 UIImagePickerController 圖片選取器用法完整整理

編輯:iOS 開發綜合

UIImagePickerController 負責管理一套可自訂的系統介面,用來取得裝置上的照片與影片,也能讓使用者在 App 內挑選已儲存的媒體。它管理使用者互動,再把結果交給 delegate 處理。這個類別不可被繼承或修改,唯一的例外是 cameraOverlayView——你可以在相機預覽上疊加自己的自訂視圖。

一、常用屬性

(1)sourceType — 決定選取介面的形式,共三種:

enum {
    UIImagePickerControllerSourceTypePhotoLibrary,   // 從相簿中挑選照片或影片
    UIImagePickerControllerSourceTypeCamera,         // 直接拍照或錄影
    UIImagePickerControllerSourceTypeSavedPhotosAlbum // 從「我的相簿」挑選
};
typedef NSUInteger UIImagePickerControllerSourceType;

(2)mediaTypes — 預設值為 kUTTypeImage,也就是只允許挑選/拍攝靜態圖片。若同時設為 kUTTypeImage 與 kUTTypeMovie(需裝置支援),則圖片與影片都能操作。使用這些常數時需引入:

#import <MobileCoreServices/MobileCoreServices.h>

(3)allowsEditing — 使用者能否編輯選取的媒體,預設 NO。

(4)cameraOverlayView — 顯示於預設選取介面最上層的自訂視圖。

二、基本使用流程

1. 先用 isSourceTypeAvailable: 判斷裝置是否支援該 sourceType(模擬器沒有相機,這步不能省)。
2. 若支援,用 availableMediaTypesForSourceType: 查詢該來源支援哪些 mediaTypes。
3. 若需調整 mediaTypes(預設只有 kUTTypeImage),在此處理。
4. 以 modal 方式呈現。
5. 使用者完成操作(取消或已選取)後,delegate 方法被觸發,關閉介面並處理結果。

三、相機相關屬性

這幾個只在 UIImagePickerControllerSourceTypeCamera 下有效:

videoQuality — 影片品質,預設 Medium。若挑選的是既存影片且畫質高於設定值,系統會將其轉檔為較低品質。

videoMaximumDuration — 最長錄影時間,預設 10 分鐘。

cameraViewTransform — 對相機預覽畫面做幾何轉換。

cameraDevice — 前/後鏡頭:

enum {
    UIImagePickerControllerCameraDeviceRear,   // 後置
    UIImagePickerControllerCameraDeviceFront   // 前置
};
typedef NSUInteger UIImagePickerControllerCameraDevice;

cameraCaptureMode — 開啟時的模式:

enum {
    UIImagePickerControllerCameraCaptureModePhoto,  // 拍照(預設)
    UIImagePickerControllerCameraCaptureModeVideo   // 錄影
};
typedef NSUInteger UIImagePickerControllerCameraCaptureMode;

cameraFlashMode — 閃光燈:

enum {
    UIImagePickerControllerCameraFlashModeOff  = -1,
    UIImagePickerControllerCameraFlashModeAuto =  0,
    UIImagePickerControllerCameraFlashModeOn   =  1
};
typedef NSInteger UIImagePickerControllerCameraFlashMode;

四、Delegate 方法與 info 字典

選好之後,結果都放在 info 這個字典裡,常見的鍵如下:

UIImagePickerControllerMediaType      // NSString,UTI 例如 kUTTypeImage
UIImagePickerControllerOriginalImage  // UIImage,原始圖
UIImagePickerControllerEditedImage    // UIImage,編輯後的圖
UIImagePickerControllerCropRect       // NSValue(CGRect),裁切區域
UIImagePickerControllerMediaURL       // NSURL,媒體檔案位置
UIImagePickerControllerReferenceURL   // NSURL,指向 AssetsLibrary 中的資源(iOS 4.1+)
UIImagePickerControllerMediaMetadata  // NSDictionary,拍攝照片的 metadata(iOS 4.1+)

完整的回呼實作:

// 使用者選好媒體後的回呼
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    // 取得編輯後的圖片(若無編輯則需退回 OriginalImage)
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (!image) {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }

    // 轉為 NSData 以便寫檔;先判斷格式
    NSData *imageData;
    if (UIImagePNGRepresentation(image)) {
        imageData = UIImagePNGRepresentation(image);          // PNG
    } else {
        imageData = UIImageJPEGRepresentation(image, 1.0);    // JPEG
    }

    // 路徑拼接後寫入沙盒
    NSString *imageSavePath = [[[HMTMySqliteDataHandle shareInstance] saveImagesPath]
                               stringByAppendingPathComponent:@"自定義.自定義"];
    [imageData writeToFile:imageSavePath atomically:YES];

    [self dismissViewControllerAnimated:YES completion:nil];
}

// 使用者取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

一個實務上的判斷技巧:與其用 if (UIImagePNGRepresentation(image)) 來試探格式(這會額外做一次完整的編碼,對大圖很浪費),不如直接檢查 info 裡的 UIImagePickerControllerMediaType,或直接固定用 JPEG 編碼並搭配適當的壓縮品質。

另外,dismissModalViewControllerAnimated: 在 iOS 6 之後已被取代,新專案請一律改用 dismissViewControllerAnimated:completion:。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved