標準流程
- 彈出
UIActionSheet (iOS 8-) 或 UIAlertController (iOS 8+) 選擇來源
- 根據選擇創建
UIImagePickerController
- 設置
sourceType、mediaTypes、allowsEditing、delegate
- present picker
- Delegate 中處理結果、dismiss
現代封裝 (iOS 8+ UIAlertController)
@interface MediaPicker : NSObject
@property (copy) void (^completion)(UIImage *image, NSURL *videoURL, NSDictionary *info);
+ (instancetype)sharedInstance;
- (void)presentFrom:(UIViewController *)vc
allowsEditing:(BOOL)edit
completion:(void (^)(UIImage *, NSURL *, NSDictionary *))completion;
@end
@implementation MediaPicker
+ (instancetype)sharedInstance {
static MediaPicker *instance;
static dispatch_once_t once;
dispatch_once(&once, ^{ instance = [[self alloc] init]; });
return instance;
}
- (void)presentFrom:(UIViewController *)vc allowsEditing:(BOOL)edit completion:(void (^)(UIImage *, NSURL *, NSDictionary *))block {
self.completion = block;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self launchPicker:UIImagePickerControllerSourceTypeCamera mediaTypes:@[(NSString *)kUTTypeImage] allowsEditing:edit];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"攝像" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self launchPicker:UIImagePickerControllerSourceTypeCamera mediaTypes:@[(NSString *)kUTTypeMovie] allowsEditing:NO];
}]];
}
[alert addAction:[UIAlertAction actionWithTitle:@"從相冊選擇" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self launchPicker:UIImagePickerControllerSourceTypePhotoLibrary mediaTypes:@[(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie] allowsEditing:edit];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
// iPad 必須設置 popoverPresentationController
if (alert.popoverPresentationController) {
alert.popoverPresentationController.sourceView = vc.view;
alert.popoverPresentationController.sourceRect = CGRectMake(vc.view.bounds.size.width/2, vc.view.bounds.size.height, 1, 1);
}
[vc presentViewController:alert animated:YES completion:nil];
}
- (void)launchPicker:(UIImagePickerControllerSourceType)source mediaTypes:(NSArray *)types allowsEditing:(BOOL)edit {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = source;
picker.mediaTypes = types;
picker.allowsEditing = edit;
picker.delegate = self;
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:picker animated:YES completion:nil];
}
#pragma mark - Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage] ?: info[UIImagePickerControllerOriginalImage];
NSURL *videoURL = info[UIImagePickerControllerMediaURL];
self.completion(image, videoURL, info);
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
self.completion(nil, nil, @{@"cancelled": @YES});
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end
調用示例
[[MediaPicker sharedInstance] presentFrom:self allowsEditing:YES completion:^(UIImage *img, NSURL *url, NSDictionary *info) {
if (img) {
self.avatarImageView.image = img;
// 上傳頭像...
} else if (url) {
// 視頻路徑 url,可用 AVAssetExportSession 壓縮
}
}];
Info.plist 必加權限 (iOS 10+)
NSCameraUsageDescription
需要相機權限拍照/攝像
NSPhotoLibraryUsageDescription
需要相冊權限選擇照片/視頻
NSMicrophoneUsageDescription
攝像時需要麥克風權限錄音
常見坑點
- ❌ 未檢查
isSourceTypeAvailable: → 模擬器無相機直接崩潰
- ❌ iPad 上 ActionSheet 無
popoverPresentationController → 崩潰
- ❌ Delegate 方法未在主線程更新 UI
- ❌ 大視頻未壓縮直接上傳 → OOM/超時
- ✅ 使用
PHPickerViewController (iOS 14+) 替代相冊選擇:無需權限、支援多選、隱私更好