Dana
Dana

Reputation: 435

UIImagePickerViewController - Photo taken in landscape creates black bar at top

I am using the UIImagePickerViewController (UIImagePickerControllerSourceTypeCamera) to take a photo in my app. However when the photo is taken in landscape, a large black bar appears on top of the image in the preview/cropping view (there's a slim black bar on the bottom as well). Like so -

enter image description here

Here's my code -

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];

In contrast, when a landscape photo is picked from the Photo Library it is placed in the centre of the preview with even black bars on top and bottom.

Where is this coming from? How do I get rid of it?

Upvotes: 7

Views: 429

Answers (2)

Andres Gutierrez
Andres Gutierrez

Reputation: 1

steamx's answer, but for Swift worked for me:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    let cropRect = (info[UIImagePickerController.InfoKey.cropRect] as? CGRect)!
    
    if cropRect.origin.y < 0 {
        self.selectedImage = info[.originalImage] as? UIImage
    } else {
        self.selectedImage = info[.editedImage] as? UIImage
    }
    
    self.dismiss(animated: true, completion: nil)
}

We check if the photo the User took was edited/cropped or not. If not, then we use the info[.originalImage]

Upvotes: 0

Vijay
Vijay

Reputation: 1016

From this Link

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker  didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *img = nil;

CGRect cropRect = [[info valueForKey:UIImagePickerControllerCropRect] CGRectValue];

if (cropRect.origin.y < 0) {

    img = [info objectForKey:UIImagePickerControllerOriginalImage];

} else {

    img = [info objectForKey:UIImagePickerControllerEditedImage];

}

}

Upvotes: 0

Related Questions