Ios 8 UIIMagePickerController showing black preview screen alternating time in iPhone 6

In my application, click the opening UIButton am.Once camera, when the camera is open, a black preview is displayed instead of the captured image, but the Retake and Use photos buttons are visible. This back preview is shown for the camera in iPhone6, but works fine on iPhone 5, 5. In my app, when the user clicks the button with photos, I go to another UIViewController. The captured image, which is stored in a variable and will be passed to another UIViewController. From this UIViewController it will be sent to the server.What causes the black preview screen, I cant 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;
}

      

+3


source to share


2 answers


first try to check the permissions for the application in the phone settings. I faced such a problem with a black screen and the camera refusal permission turned off. I didn't understand how this happened because I assumed I allowed the camera.

Also you better check the resolution before presenting the 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];
            }
        }];
    }

      

+2


source


This is an old but good answer, I also ran into the black camera preview issue mostly on iOS7. You can fix your problem. First you need to comment out the code that is executed after capturing the image. Now, when the camera just opens, takes a picture, and you reject it without image processing, you will notice that the UIImagePickerController is working fine or not. Now uncomment the image processing code, you will get a black preview after a few shots are most likely taken and processed. If that works, start adding some of the image processing code, otherwise see how you present the UIImagePickerController, you don't need to call addOperationWithBlock as the click button is already on the main thread. Get rid of this challengedo it ok. Make sure you don't do anything funny with asynchronous calls after the snapshot is taken. In my case, I had multiple threads executing after the snapshot was taken, causing the next attempt at currentViewController to fail with a black preview. After I fixed it so that the asynchronous activity was the only thread doing the background image processing, it worked as expected all the time.so that the asynchronous activity is the only thread doing the background image processing, it works as expected all the time.so that the asynchronous activity is the only thread doing the background image processing, it works as expected all the time.



0


source







All Articles