您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

UIImagePickerController 完整指南:拍照、選圖、視頻與代理回調

編輯:iOS 相關教學

UIImagePickerController 提供了一套系統級的、可自定義的界面,用於從設備相冊中挑選照片或視頻,或者直接調起相機拍攝。它負責管理全部用戶交互,再把結果交給代理對象處理。

這個類幾乎不能被繼承或修改,唯一允許的定制點是 cameraOverlayView——你可以在取景畫面上疊加自定義層。

一、常用屬性

sourceType:界面來源

typedef NS_ENUM(NSUInteger, UIImagePickerControllerSourceType) {
    UIImagePickerControllerSourceTypePhotoLibrary,   // 從相冊中選
    UIImagePickerControllerSourceTypeCamera,         // 調起相機拍攝
    UIImagePickerControllerSourceTypeSavedPhotosAlbum // 從相機膠卷中選
};

mediaTypes:媒體類型

默認值為 kUTTypeImage,即只允許靜態圖片。若設備支持,可同時放入 kUTTypeImage 與 kUTTypeMovie,讓用戶既能選圖也能選視頻。使用這兩個常量需要引入:

#import <MobileCoreServices/MobileCoreServices.h>

allowEditing 與 cameraOverlayView

@property (nonatomic) BOOL allowEditing;              // 是否允許裁剪編輯,默認 NO
@property (nonatomic, strong) UIView *cameraOverlayView; // 疊加在取景界面最上層

二、標準使用流程

  1. 用 isSourceTypeAvailable: 判斷當前設備是否支持該來源(模擬器沒有相機)。
  2. 若支持,用 availableMediaTypesForSourceType: 查詢該來源支持的媒體類型。
  3. 按需設置 mediaTypes。
  4. 以 modal 方式彈出界面。
  5. 用戶完成選擇或取消後,代理方法被觸發,關閉界面並處理數據。

三、相機相關的專屬屬性

// 視頻質量,默認 medium。選中已存在的視頻時,若其質量更高,會被轉碼為該質量
@property (nonatomic) UIImagePickerControllerQualityType videoQuality;

// 最大錄製時長,默認 10 分鐘
@property (nonatomic) NSTimeInterval videoMaximumDuration;

// 取景畫面的變換矩陣
@property (nonatomic) CGAffineTransform cameraViewTransform;

// 使用哪個攝像頭
typedef NS_ENUM(NSUInteger, UIImagePickerControllerCameraDevice) {
    UIImagePickerControllerCameraDeviceRear,   // 後置
    UIImagePickerControllerCameraDeviceFront   // 前置
};

// 打開相機時的默認模式
typedef NS_ENUM(NSUInteger, UIImagePickerControllerCameraCaptureMode) {
    UIImagePickerControllerCameraCaptureModePhoto,   // 拍照
    UIImagePickerControllerCameraCaptureModeVideo    // 錄像
};

// 閃光燈
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
    UIImagePickerControllerCameraFlashModeOff  = -1,
    UIImagePickerControllerCameraFlashModeAuto =  0,
    UIImagePickerControllerCameraFlashModeOn   =  1
};

四、代理回調與 info 字典

用戶選好之後,結果全部封裝在 info 字典裡,常見的鍵如下:

UIImagePickerControllerMediaType      // 媒體類型(UTI 字符串)
UIImagePickerControllerOriginalImage   // 原始圖片(UIImage)
UIImagePickerControllerEditedImage     // 裁剪後的圖片(UIImage)
UIImagePickerControllerCropRect        // 裁剪區域(NSValue 包裹的 CGRect)
UIImagePickerControllerMediaURL        // 視頻文件的 URL(NSURL)
UIImagePickerControllerReferenceURL    // 引用相冊資產的 URL
UIImagePickerControllerMediaMetadata   // 拍攝照片的元數據字典

完整的回調實現:

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {

    // 來自相機的原始圖,可先留一份
    UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    // 優先取編輯後的圖
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

    // 轉為 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];
}

實踐提醒

示例中使用的 dismissModalViewControllerAnimated: 早已被廢棄,應改用 dismissViewControllerAnimated:completion:。此外,UIImagePickerController 的相冊訪問屬於「系統級授權」,不需要在 Info.plist 中聲明相冊權限;但一旦使用 sourceType 為 Camera,就必須配置 NSCameraUsageDescription,錄制視頻還需要 NSMicrophoneUsageDescription。最後,選圖之後一定要及時處理大圖的內存問題——直接持有原圖可能導致內存峰值過高,建議按需降採樣或異步解碼。

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