我已经实现了以下功能并且相机已打开,但应用程序已崩溃,有时打开并拍摄照片然后应用程序被破解并且仅显示日志“快照未渲染的视图会导致空白快照。确保您的视图在屏幕更新后快照或快照之前至少渲染一次收到内存警告。“
我使用的相同功能从图库及其工作中获取图像。
-(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];
}
任何人都可以提供帮助。
答案 0 :(得分: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
答案 1 :(得分:0)
- (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];
}
}
答案 2 :(得分:0)
[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];
}
}