| 架構 | 類別 | 特點 | 適用場景 |
|---|---|---|---|
| 高層 API | UIImagePickerController | 系統 UI、零配置、支援相簿選擇、編輯裁剪 | 標準拍照/選圖需求 |
| 底層 API | AVFoundation (AVCaptureSession) | 完全自定義 UI、即時濾鏡、條碼掃描、手電筒控制 | 自定義相機、AR、掃碼、手電筒 |
本文聚焦 **UIImagePickerController 拍照/選圖** + **AVFoundation 手電筒控制**。
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// 模擬器無相機、裝置無相機權限
[self showAlert:@"無法使用相機"];
return;
}
- (void)openCamera {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self; // 需遵守 UINavigationControllerDelegate, UIImagePickerControllerDelegate
picker.allowsEditing = YES; // 允許拍完編輯/裁剪
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto; // 自動閃光燈
[self presentViewController:picker animated:YES completion:nil];
}
// Delegate: 拍攝完成
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
// 取得編輯後圖片(或原圖)
UIImage *image = info[UIImagePickerControllerEditedImage] ?: info[UIImagePickerControllerOriginalImage];
// 儲存至相簿
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker dismissViewControllerAnimated:YES completion:nil];
}
// Delegate: 取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
// 儲存完成回調
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
NSLog(@"儲存失敗: %@", error);
} else {
NSLog(@"已儲存至相簿");
}
}
- (void)openPhotoLibrary {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
// 選圖完成回調(iOS 11+ 建議用 PHPickerViewController 替代)
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage] ?: info[UIImagePickerControllerOriginalImage];
// 使用 image...
[picker dismissViewControllerAnimated:YES completion:nil];
}
核心類:`AVCaptureDevice`、`AVCaptureSession`、`AVCaptureDeviceInput`、`AVCaptureVideoDataOutput`
@property (nonatomic, strong) AVCaptureSession *torchSession;
- (void)turnOnTorch {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (!device.hasTorch) {
NSLog(@"裝置不支援手電筒");
return;
}
@try {
[device lockForConfiguration:nil];
// 關鍵:建立 Session 並啟動,手電筒才會持續亮燈
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[session addInput:input];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
[session beginConfiguration];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
[session commitConfiguration];
[session startRunning];
self.torchSession = session; // 持有強引用,防止釋放導致熄滅
}
@catch (NSException *e) {
NSLog(@"開啟手電筒失敗: %@", e);
}
}
- (void)turnOffTorch {
if (self.torchSession) {
[self.torchSession stopRunning];
self.torchSession = nil; // 釋放 Session,裝置自動恢復
}
}
- (IBAction)toggleTorch:(id)sender {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (!device.hasTorch) return;
if (device.torchMode == AVCaptureTorchModeOff) {
[self turnOnTorch];
} else {
[self turnOffTorch];
}
}
NSCameraUsageDescription
需要相機權限拍照上傳頭像
NSPhotoLibraryUsageDescription
需要相簿權限選擇圖片
NSPhotoLibraryAddUsageDescription
需要權限儲存拍攝照片至相簿