UIImagePickerController是iOS中自带的系统相册选择器, 使用起来非常简便.
UIImagePickerController
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
// 设置sourceType为系统相册, 如果使用Camera请对应修改该属性.
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
UIImagePickerController本身继承自UINavigationController, 因此不能将其简单视作一个UIViewController来看待.
了UINavigationControllerDelegate和UIImagePickerControllerDelegate协议, 包含了由三个ViewController组成的导航视图, 我们通过打印其viewControllers即可看出来:
(lldb) po [picker viewControllers]
<__NSArrayI 0x146b31140>(
<PUUIAlbumListViewController: 0x14609a200>,
<PUUIPhotosAlbumViewController: 0x1458e5a00>,
<PUUIImageViewController: 0x1468f1ad0>
)
UIImagePickerControllerDelegate
UIImagePickerControllerDelegate协议有如下三个代理方法.
__TVOS_PROHIBITED @protocol UIImagePickerControllerDelegate<NSObject>
@optional
// The picker does not dismiss itself; the client dismisses it in these callbacks.
// The delegate will receive one or the other, but not both, depending whether the user
// confirms or cancels.
- (void)imagePickerController:(UIImagePickerControll