在我的应用程序中点击UIButton am打开相机。一旦打开相机,将显示黑色预览而不是捕获图像,但是可以看到重新拍摄和使用照片按钮。此背面预览显示在iPhone6中的相机但是在iPhone 5,5s中工作正常。在我的应用程序中,一旦用户点击使用照片按钮,我就导航到其他UIViewController。捕获的图像,存储在变量中,并将传递给另一个UIViewController。从这个UIViewController它将被发布到server.What导致黑色预览屏幕无法弄清楚。寻求帮助。我的代码是:
ON button click
{
[self takeNewPhotoFromCamera];
}
- (void)takeNewPhotoFromCamera
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = NO;
//controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
controller.delegate = self;
[self callOperationQue];
}
}
- (void)callOperationQue{
if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.navigationController presentViewController: controller animated: YES completion: nil];
}];
}
else
{
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
UIImage *image1 = [info valueForKey: UIImagePickerControllerOriginalImage];
// imageData = UIImagePNGRepresentation(image1);
UIImage *newImage = [self squareImageWithImage:image1 scaledToSize:sz];
imgVwProfile.image=newImage;
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 250*1024;
imageData = UIImageJPEGRepresentation(newImage, compression);
while ([imageData length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1;
imageData = UIImageJPEGRepresentation(newImage, compression);
}
//passing image data to other UIViewController
CreateClaimViewController *address=[[CreateClaimViewController alloc]init];
address.img=newImage;
[self.navigationController pushViewController:address animated:NO];
}
//resizing of image
- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
double ratio;
double delta;
CGPoint offset;
//make a new square size, that is the resized imaged width
CGSize sz = CGSizeMake(newSize.width, newSize.width);
//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (image.size.width > image.size.height) {
ratio = newSize.width / image.size.width;
delta = (ratio*image.size.width - ratio*image.size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = newSize.width / image.size.height;
delta = (ratio*image.size.height - ratio*image.size.width);
offset = CGPointMake(0, delta/2);
}
//make the final clipping rect based on the calculated values
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * image.size.width) + delta,
(ratio * image.size.height) + delta);
//start a new context, with scale factor 0.0 so retina displays get
//high quality image
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
} else
{
UIGraphicsBeginImageContext(sz);
}
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
答案 0 :(得分:2)
首先尝试在手机设置中检查应用的权限。我遇到了黑屏的问题,相机拒绝许可。我不明白它是怎么发生的,因为我以为我允许相机。
此外,您最好在呈现控制器之前检查权限
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(status == AVAuthorizationStatusAuthorized) { // authorized
[self takeFoto];
}
else if(status == AVAuthorizationStatusDenied){ // denied
[self showCameraDeniedError];
}
else if(status == AVAuthorizationStatusRestricted){ // restricted
[self showCameraDeniedError];
}
else if(status == AVAuthorizationStatusNotDetermined){ // not determined
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){ // Access has been granted ..do something
[self takeFoto];
} else {
[self showCameraDeniedError];
}
}];
}
答案 1 :(得分:0)
这是一个陈旧但很好的答案,我也面临黑色相机预览问题,主要是在iOS7上。你可以解决你的问题。首先,您需要注释掉捕获图片后执行的代码。既然相机刚刚打开,拍摄照片,并且在没有处理图像的情况下将其关闭,您会注意到UIImagePickerController的工作正常与否。现在取消注释图像处理代码,在拍摄并最有可能处理几张照片后,您将获得黑色预览。如果这样做,开始添加一些图像处理代码,否则看看你如何呈现UIImagePickerController,你不需要调用addOperationWithBlock,因为按钮点击已经在主线程上。摆脱那个电话,正常做。在拍摄照片后,确保你没有做任何有趣的异步调用。在我的情况下,我拍摄了一张照片后执行了多个线程,这导致下一个presentViewController尝试失败,黑色预览。在我修复它之后,异步操作是一个执行背景图像处理的单个线程,它一直在正常工作。