简体   繁体   English

如何检测iPhone相机没有拍摄UIImage?

[英]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: 我目前正在使用UIImageimageOrientation属性来确定在相机拍摄照片后是否将照片翻转为横向:

//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? 有没有办法检测UIImage是由手机的相机拍摄还是来自相机设备?
  2. Is there a way to implicitly determine to see whether an UIImage is taken landscape or portrait? 有没有办法隐式确定UIImage是采用横向还是纵向?

Thanks! 谢谢!

for #1, use this solution to get the EXIF dictionary associated with the UIImage. 对于#1,使用此解决方案获取与UIImage关联的EXIF字典。 There are a lot of keys there that identifies the camera. 那里有很多键可以识别相机。

For #2 对于#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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM