这个组件跟PopoverController一样,也是直接使用,不需要也不允许sub-classing的类,但是相对来说比PopoverController要复杂一些
Important:The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.
开发者不是通过继承的方式来定义此控件的行为,而是通过设置它的属性,以及实现其delegate方法
下面是最简单的示例代码,可以读取本地相册中的照片和视频:
- (void)buttonPressed { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; imagePicker.allowsEditing = YES; popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker]; popoverController.delegate = self; [popoverController presentPopoverFromRect:CGRectMake(20, 20, 400, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; }
下面总结ImagePicker最重要的几个属性:
sourceType,该属性决定ImagePicker的功能,只有3个枚举值。该属性是必须设置的
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) { UIImagePickerControllerSourceTypePhotoLibrary, UIImagePickerControllerSourceTypeCamera, UIImagePickerControllerSourceTypeSavedPhotosAlbum };
allowsEditing,此属性默认为NO,那么选中照片之后,就会立刻调用delegate方法(见下文),如果设置为YES,则会进入图片编辑的界面
delegate,设置delegate,一般是设置为当前的ViewController。此对象不仅要实现ImagePickerControllerDelegate,还需要实现NavigationControllerDelegate,因为ImagePickerController是继承自NavigationController的
@property(nonatomic,assign) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
一般还需要实现这个方法:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [[UIApplication sharedApplication] setStatusBarHidden:YES]; }