相机在IOS中不工作
Posted
技术标签:
【中文标题】相机在IOS中不工作【英文标题】:Camera is not working in IOS 【发布时间】:2016-02-20 12:27:53 【问题描述】:我已经实现了以下操作并打开了相机但应用程序崩溃了,有时打开并拍照时应用程序崩溃并且日志仅显示“对未渲染的视图进行快照会导致空快照。确保您的视图在快照或屏幕更新后的快照之前已至少渲染一次收到内存警告。”
我用于从图库中获取图像及其工作的相同功能。
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex
if (buttonIndex==0)
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Device has no Camera!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
else
[self performSelector:@selector(loadCamera) withObject:nil afterDelay:1.0];
-(void)loadCamera
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
picker1.allowsEditing=YES;
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion:NULL];
谁能帮忙。
【问题讨论】:
请参考:我认为它很有帮助Open Camera in ios CJ 现在你得到解决方案了吗? 你试过下面的答案了吗?如果没有,告诉我。 我使用了下面的,但应用程序仍然崩溃并且还使用它的代表 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 我在 actionsheet 动作下调用这个相机功能。这有什么问题吗? 【参考方案1】:通过以下代码解决:
-(void)loadCamera
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
dispatch_async(dispatch_get_main_queue(), ^
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
picker1.allowsEditing=YES;
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion: nil];
);
此代码适用于 iOS 9.2 和 xCode 7.2
【讨论】:
同样在 ios 12.1 中,这是 appstore 向我的应用程序报告的错误。我添加了 dispatch_async 并开始工作。谢谢【参考方案2】:- (IBAction)takeAPhoto:(VSButton *)sender
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
else
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
【讨论】:
presentModalViewController 在 iOS 6 中已弃用。建议使用 presentViewController@Muhammad Raheel Mateen 我将粘贴方法,这也在我的项目中运行。 它的显示错误和应用程序崩溃 > 对未渲染的视图进行快照会导致快照为空。确保您的视图在快照或屏幕更新后至少渲染一次。【参考方案3】:[self performSelector:@selector(loadCamera) withObject:nil afterDelay:1.0];
-(void)loadCamera
if ([UIIagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera)
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
picker1.allowsEditing=YES;
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion:NULL];
【讨论】:
我使用了上述方法,但应用程序仍然崩溃并且还使用了它的委托 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info以上是关于相机在IOS中不工作的主要内容,如果未能解决你的问题,请参考以下文章
UIButton 在相机流中不可见 (Xamarin.iOS)
相机插件在 iOS 的 phonegap 应用程序中不起作用