我尝试使用以下代码创建自定义UIImagePickerController
。
的 ECPhotoPickerController.h
@interface ECPhotoPickerController : UIImagePickerController
@property (strong, nonatomic) void(^didFinishSavingPhoto)(NSURL* url, UIImage* image);
@property (strong, nonatomic) void(^didDismissed)(void);
@end
ECPhotoPickerController.m
...
- (void)viewDidLoad {
[super viewDidLoad];
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.showsCameraControls = false;
[self setCameraOverlayView:self.overlayView];
self.delegate = self;
}
...
点击按钮后,将显示自定义图像选择器控制器,这将调用以下方法。
- (void)showImagePicker {
ECPhotoPickerController* vc = [[ECPhotoPickerController alloc] init];
vc.didFinishSavingPhoto = ^(NSURL *url, UIImage *image) {
self.selectedImage = image;
};
vc.didDismissed = ^{
// dismissed
};
[self presentViewController:vc animated:true completion:nil];
}
但该应用将 在iOS 11上以日志
崩溃[UIViewController _setCameraOverlayView:]: unrecognized selector sent to instance 0x108403dd0
并且在崩溃之前有另一个奇怪的日志。
[] <<<< AVOutputDeviceDiscoverySession (FigRouteDiscoverer) >>>> -[AVFigRouteDiscovererOutputDeviceDiscoverySessionImpl outputDeviceDiscoverySessionDidChangeDiscoveryMode:]: Setting device discovery mode to DiscoveryMode_None (client: ECalligraphy)
自定义图像选取器类显然继承自UIImagePickerController
当我用[[ECPhotoPickerController alloc] init]
初始化它时,它应该调用超类的初始化,因为我没有实现ECPhotoPickerController
的初始化。
我对此次崩溃感到非常困惑。 任何建议将不胜感激。
答案 0 :(得分:0)
我最终找到了原因!
应在视图控制器开始之前指定sourceType
在iOS 11上加载。
这意味着,您应该在自定义视图控制器初始化期间设置sourceType
的值,或者在加载前为实例执行此操作。
- (instancetype)init { // `initFromNibName` etc. depends on the way you initialize it.
if (self = [super init]) {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.showsCameraControls = false;
self.delegate = self;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
或
- (void)showImagePicker {
ECPhotoPickerController* vc = [[ECPhotoPickerController alloc] init];
vc.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:vc animated:true completion:nil];
}