iOS 8中的UIImagePickerController在iPhone 6上交替显示黑色预览屏幕

3

在我的应用程序中,单击UIButton时将打开相机。一旦相机打开,预览窗口会显示黑色而不是捕获的图像,但重新拍摄和使用照片按钮是可见的。这个黑色的预览在iPhone6的相机中显示,在iPhone 5、5s上正常工作。一旦用户点击使用照片按钮,我的应用程序将导航到另一个UIViewController。捕获的图像存储在变量中,并将传递到另一个UIViewController。从这个UIViewController它将被发送到服务器。我无法确定是什么原因导致了黑色的预览屏幕。希望得到帮助。以下是我的代码:

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;
}

如果在iPhone 5上存在内存泄漏,它会如何正常工作。 - user2552751
1
我在两个不同的应用程序上遇到了相同的问题。我想知道这是否是iOS8的一个bug,但对我来说似乎太大了,难以被苹果公司的人员注意到。也许是因为我仍在使用Xcode5进行编译? - Martin
我正在使用Xcode 6,但是仍然遇到了这个问题。 - user2552751
我的iPhone6 iOS8.1也有同样的问题。 - fvisticot
1
我在iPad 2上遇到了同样的问题,但在iPad 3上没有。我刚刚尝试恢复iPad 2,问题得到了解决。 - tagy22
显示剩余3条评论
2个回答

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];
            }
        }];
    }

我正在使用UIImagePickerController,AVAuthorizationSatus是否适用于它? - user2552751
@user2552751 是的,它可以。我也用它来拍照。然而,它只能在iOS7或更高版本上运行。如果你支持早期版本,你可以省略权限检查,因为那里不可用。 - Nik Yekimov
我只在iOS10和横屏模式下遇到了这个问题。有什么看法吗? - nOOb iOS

0

这是一个古老但好的答案,我也遇到了黑色相机预览问题,主要是在iOS7上。你可以解决你的问题。首先,你需要注释掉在拍照后执行的代码。现在相机只是打开,拍照,然后你关闭它而不处理图像,你会发现UIImagePickerController工作得很好或者不好。现在取消注释图像处理代码,你将在拍摄和处理几张图片后得到黑色预览。如果这个方法有效,开始添加一些图像处理代码,否则看看你如何呈现UIImagePickerController,因为你不需要调用addOperationWithBlock,因为按钮点击已经在主线程上了。去掉那个调用,正常做就行了。确保在拍照后没有进行任何有趣的异步调用。在我的情况下,我在拍照后有多个线程执行,导致下一个presentViewController尝试失败并出现黑色预览。当我修复它,使异步操作成为单个线程执行背景图像处理时,它始终正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接