Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios 8 UIIMagePickerController showing a black preview screen alternate times in iPhone 6

In my app on click of a UIButton am opening camera.Once the camera is opened a black preview is shown instead of the capturing image,but Retake and Use photos buttons are visible.This back preview is shown for camera in iPhone6 but works fine in iPhone 5, 5s.In my app once the user clicks use photo button,am navigating to other UIViewController.The captured image, which is stored in variable and will be passed to another UIViewController.From this UIViewController it will be posted to server.What is causing the black preview screen am not able to figure out.Looking for help.Below is my code:

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;
}
like image 393
user2552751 Avatar asked Nov 27 '25 04:11

user2552751


1 Answers

try to check app's permissions in phone settings first. I have experienced such issue with black screen and the camera deny permission was on. I did not understand how it happened, because I assumed that I allowed camera.

Also you'd better to check permission before presenting controller

 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];
            }
        }];
    }
like image 68
Nik Yekimov Avatar answered Nov 28 '25 19:11

Nik Yekimov