iOS 有兩條拍照與選圖的路:一是 UIImagePickerController,系統提供的現成介面,幾行就能完成;二是 AVFoundation,可以完全自訂取景畫面與選圖流程。本文示範第一種,並加上手動控制補光燈。
調用前應該先確認能力是否可用,避免在使用者關閉權限或裝置不支援時崩潰:
isSourceTypeAvailable: 判斷來源是否可用(相機、相簿)availableMediaTypesForSourceType: 查該來源支援的媒體類型使用 UIImagePickerController 需實作兩個 delegate:UINavigationControllerDelegate 與 UIImagePickerControllerDelegate。若要控制閃光燈,還要引入 AVFoundation 並建立 AVCaptureSession。
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface CameraViewController : UIViewController
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
AVCaptureSession *_AVSession; // 控制閃光燈用
}
@property (nonatomic, retain) AVCaptureSession *AVSession;
@end
-(void)addCarema {
// 模擬器不支援相機,需先判斷
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"你沒有攝像頭"
delegate:nil
cancelButtonTitle:@"Drat!"
otherButtonTitles:nil];
[alert show];
}
}
// 拍攝完成
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); // 存入相簿
[self dismissModalViewControllerAnimated:YES];
}
// 按下取消
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
-(void)openPicLibrary {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"無法開啟相簿"
delegate:nil
cancelButtonTitle:@"Drat!"
otherButtonTitles:nil];
[alert show];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[self dismissModalViewControllerAnimated:YES];
}
-(void)openFlashlight {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device.torchMode == AVCaptureTorchModeOff) {
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 lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn]; // 開啟補光燈
[device unlockForConfiguration];
[session commitConfiguration];
[session startRunning];
[self setAVSession:session]; // 保留 session 參照,關閉時才停得掉
[output release];
}
}
-(void)closeFlashlight {
[self.AVSession stopRunning];
[self.AVSession release];
}
實測時有個現象:開燈的瞬間會先閃一下才穩定亮起,這是 torch 模式切換的正常過渡。另外提醒兩點:一是 AVCaptureSession 必須強引用保存,否則 session 被回收後燈會自己熄掉;二是補光燈很耗電,離開頁面時記得呼叫 closeFlashlight,並在 dealloc 或 viewWillDisappear 中做好收尾。
最後,以上寫法屬於早期 API。新專案建議改用 presentViewController:animated:completion: 取代已棄用的 presentModalViewController,並搭配 PHPickerViewController 處理相簿選取;閃光燈控制則可用 AVCaptureDevice 的 setTorchModeActiveLevel 做更細的亮度調整。