調用相機與相簿是幾乎每個 App 都會遇到的需求。本文把.delegate 設定、iPhone 與 iPad 的差異處理,以及自訂相機界面的做法整理在一起。
使用 UIImagePickerController 時,你的類別必須同時宣告遵循:
@interface AddSightingViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@end
少了 UINavigationControllerDelegate,Xcode 會跳出類似以下的警告:
Assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>' from incompatible type 'AddSightingViewController *const __strong'
這裡要澄清一個常見誤解:這個警告的真正原因不是 strong 與 weak 不相容,而是型別不相容——编译器發現你的物件並未宣告符合 UIImagePickerController 要求的協定。網路上流傳「把 picker 屬性從 strong 改成 weak 警告就消失」的做法,其實只是換了另一種寫法讓警告沒有被觸發,並沒有解決型別契約的問題;而且 picker 由 self 呈現、生命週期短,用 strong 或 weak 都可以,重點是協定要宣告對。
另外,Xcode 有時不會即時更新警告狀態,改完後做一次 Product → Clean 是個好習慣。
- (void)takePhotoAction:(UIButton *)sender {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
}
sourceType 有三種可選值:相機(Camera)、相簿(PhotoLibrary)、已儲存相片(SavedPhotosAlbum)。iPod touch 沒有相機,所以務必先呼叫 isSourceTypeAvailable: 判斷,再決定要退回相簿還是被下一個錯誤訊息,這是實務上最容易被忽略的一行防禦。
- (void)openPhotosAction:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [UIImagePickerController
availableMediaTypesForSourceType:picker.sourceType];
}
picker.delegate = self;
picker.allowsEditing = NO;
[self presentViewController:picker animated:YES completion:nil];
}
在 iPad 上,相簿不能以全螢幕方式呈現,必須包在 UIPopoverController 裡。UIPopoverController 只能在 iPad 使用,其特性是永遠浮在當前畫面最上層,點擊其他區域會自動關閉。
- (void)ipadPicker {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:picker];
[popover presentPopoverFromRect:CGRectMake(0, 0, 300, 300)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
若 popover 是從導航列按鈕彈出,改用 presentPopoverFromBarButtonItem:permittedArrowDirections:animated: 即可自動對齊按鈕位置。設備旋轉後若箭頭位置跑掉,需要在 didRotateFromInterfaceOrientation: 中重新計算 rect 並再次呼叫 present 方法。
有時候我們希望以相機畫面為背景,在上面疊加自己的控件(例如自訂快門、對焦框),這時可以關掉系統控制列,並用 cameraViewTransform 縮放預覽。
- (void)openCustomCamera:(id)sender {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setSourceType:UIImagePickerControllerSourceTypeCamera];
[controller setShowsCameraControls:NO];
controller.cameraViewTransform =
CGAffineTransformMakeScale(1.25, 1.25);
[self presentViewController:controller animated:YES completion:nil];
}
若要進一步自訂,可用 AVFoundation 直接操作 AVCaptureSession,自由度遠高於 UIImagePickerController。
早期專案若相機界面顯示英文,通常是因為專案的 Localization native development region 沒有設為 China/Chinese。到 Target → Info 中把該值改為 China,重新執行即可。
別忘了實作 imagePickerController:didFinishPickingMediaWithInfo:,從 info 字典的 UIImagePickerControllerEditedImage 或 UIImagePickerControllerOriginalImage 取出影像;並記得在取消時實作 imagePickerControllerDidCancel: 把 picker 關掉,否則使用者會卡在相機畫面。