我设置了一个 UIImagePickerController
模态视图来录制视频,一旦用户选择“使用视频”,它就会关闭 View Controller 并执行我想要的操作。然而,一旦我添加了一个 cameraOverlayView,它只是一个 UIView,没有任何特别的事情发生,didFinishPickingMediaWithInfo
就永远不会被调用。即使我将 NSLog 放在该函数的第一行,我也看不到任何输出。更奇怪的是,当用户按下取消按钮时,UIImagePickerControllerDidCancel
仍然会被调用。
之前关于 SO 的建议似乎没有帮助,因为我已经设置了委托(delegate)并相应地设置了编辑属性,这与这些流行的帖子不同:
这是启动 UIImagePickerController 的代码(取自 Ray Wenderlich 教程):
-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller
usingDelegate:(id )delegate {
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
if(![delegate conformsToProtocol:@protocol(UIImagePickerControllerDelegate) ]) {
NSAssert(nil, @"delegate muste conforms to UIImagePickerControllerDelegate protocol");
}else{
NSLog(@"delegate does conform to protocol");
}
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CameraOverlay *overlay = [[CameraOverlay alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.delegate = delegate;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
cameraUI.allowsEditing = NO;
//if I comment out the line below, everything works fine.
//but if I don't comment this out, didFinishPickingMediaWithInfo is never called
cameraUI.cameraOverlayView = overlay;
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
这是 didFinishPickingMediaWithInfo
的一部分:
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"did finish picking media with info");
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
// I also tried [picker dismissModalViewControllerAnimated:NO];
//despite it not being its own delegate, but in any case
//this did not change the result
...
}
我按照 Andrey 的建议添加了有效的委托(delegate)检查,但现在我总是看到“委托(delegate)确实符合协议(protocol)”,无论覆盖是否在其中以及 didFinishPickingMediaWithInfo
是否被调用。
对于尝试什么或代码中的错误有什么建议吗?感谢您查看。
最佳答案
这是最终起作用的方法:将叠加 View 的 userInteractionEnabled
设置为 FALSE
。
我希望我知道这是错误还是功能。我查看了一些创建叠加 View 的教程,但都没有提到设置此属性。如果有人能完全解释为什么这会产生影响,我想标记一个比这更完整的答案。
关于ios - 分配 cameraOverlyView 属性时未调用 UIImagePickerController didFinishPickingMediaWithInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30919230/