分配 cameraOverlyView 属性 时未调用 UIImagePickerController didFinishPickingMediaWithInfo
UIImagePickerController didFinishPickingMediaWithInfo not being called when cameraOverlyView property is assigned
我设置了一个 UIImagePickerController
模态视图来录制视频,一旦用户选择 "Use Video" 它就会很好地关闭视图控制器并执行我想要的操作。然而,一旦我添加了一个 cameraOverlayView,它只是一个 UIView,没有任何特别的事情发生,didFinishPickingMediaWithInfo
就永远不会被调用。即使我将 NSLog 放在该函数的第一行,我也看不到任何输出。更奇怪的是,当用户按下“取消”按钮时,UIImagePickerControllerDidCancel
仍然会被调用。
之前关于 SO 的建议似乎没有帮助,因为我已经设置了委托并相应地设置了编辑属性,这与这些热门帖子不同:
uiimagepickercontroller didfinishpickingmediawithinfo NOT CALLED when selecting a video from the library
UIImagePickerController didFinishPickingMediaWithInfo not being called
这是启动 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 does conform to protocol",无论覆盖是否存在以及 didFinishPickingMediaWithInfo
是否被调用。
对于尝试什么或代码中的错误有什么建议吗?感谢您的观看。
以下是最终起作用的方法:将叠加视图的 userInteractionEnabled
设置为 FALSE
。
我希望我知道这是错误还是功能。我查看了一些创建叠加视图的教程,none 提到设置这个 属性。如果有人能完全解释为什么这会产生影响,我想标记一个比这更完整的答案。
@sunny 终于正确解决了! userInteractionEnabled = FALSE 确实调用了 didFinishPickingMediaWithInfo
我设置了一个 UIImagePickerController
模态视图来录制视频,一旦用户选择 "Use Video" 它就会很好地关闭视图控制器并执行我想要的操作。然而,一旦我添加了一个 cameraOverlayView,它只是一个 UIView,没有任何特别的事情发生,didFinishPickingMediaWithInfo
就永远不会被调用。即使我将 NSLog 放在该函数的第一行,我也看不到任何输出。更奇怪的是,当用户按下“取消”按钮时,UIImagePickerControllerDidCancel
仍然会被调用。
之前关于 SO 的建议似乎没有帮助,因为我已经设置了委托并相应地设置了编辑属性,这与这些热门帖子不同:
这是启动 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 does conform to protocol",无论覆盖是否存在以及 didFinishPickingMediaWithInfo
是否被调用。
对于尝试什么或代码中的错误有什么建议吗?感谢您的观看。
以下是最终起作用的方法:将叠加视图的 userInteractionEnabled
设置为 FALSE
。
我希望我知道这是错误还是功能。我查看了一些创建叠加视图的教程,none 提到设置这个 属性。如果有人能完全解释为什么这会产生影响,我想标记一个比这更完整的答案。
@sunny 终于正确解决了! userInteractionEnabled = FALSE 确实调用了 didFinishPickingMediaWithInfo