daspianist
daspianist

Reputation: 5495

How to detect wether an UIImage was not taken by the iPhone camera?

I am currently using the imageOrientation property of UIImage to determine whether or not to flip the picture to landscape after the picture is taken by the camera:

//3 is upright
//2 is upside-down
//1 is button on the left
//0 is button on the right


    if (self.imageTakenOrSelected.imageOrientation == 1){ //Taken landscape with button the left
        UIImage *flippedImage = [UIImage imageWithCGImage:self.imageTakenOrSelected.CGImage scale:self.imageTakenOrSelected.scale orientation:UIImageOrientationRight];
        self.imageTakenOrSelected = flippedImage;
    } else if (self.imageTakenOrSelected.imageOrientation == 0){ //Taken landscape with button the right
        UIImage *flippedImage = [UIImage imageWithCGImage:self.imageTakenOrSelected.CGImage scale:self.imageTakenOrSelected.scale orientation:UIImageOrientationRight];
        self.imageTakenOrSelected = flippedImage;
    }

Presently this works fine for pictures taken by the camera, but I noticed that when it comes to screenshotted pictures or pictures downloaded from the web, the default orientation is always set to landscape despite whether the picture is truly in landscape or not.

My questions are:

  1. Is there a way to detect whether the UIImage was taken by the phone's camera or comes from a on-camera device?
  2. Is there a way to implicitly determine to see whether an UIImage is taken landscape or portrait?

Thanks!

Upvotes: 1

Views: 214

Answers (1)

Duck
Duck

Reputation: 35953

for #1, use this solution to get the EXIF dictionary associated with the UIImage. There are a lot of keys there that identifies the camera.

For #2

just compare the image's width and height

CGFloat width  = [image width];
CGFloat height = [image height];

if (width > height) {
  // image is landscape
} else {
  // image is portrait
}

Upvotes: 3

Related Questions