【发布时间】:2011-04-13 07:38:49
【问题描述】:
谁能告诉我如何使用 UIImagePickerController(相机和相册)委托确定图像的方向、相应地旋转并将其分配给 UIImageView?
【问题讨论】:
标签: iphone cocoa-touch uiimagepickercontroller orientation exif
谁能告诉我如何使用 UIImagePickerController(相机和相册)委托确定图像的方向、相应地旋转并将其分配给 UIImageView?
【问题讨论】:
标签: iphone cocoa-touch uiimagepickercontroller orientation exif
不,UIImagePickerController 不能明确地做到这一点。
如果要根据图片的内容来判断。这是一个难题。如何在不了解图像内容的情况下确定图像的方向?
我可以建议您检查返回图像的宽度和长度,看看它是纵向还是横向。为了旋转图像,外面有一些库和代码,但我没有尝试太多。你可以看看this blog
【讨论】:
Vodkhang 的回答部分不正确 - 现代相机在拍摄照片时会将明确的方向放入图像中。这已经很常见了大约 5 年多。
我之前使用此答案中的代码通过直接从图片中读取“方向”信息来进行旋转:
UIImagePickerController camera preview is portrait in landscape app
【讨论】:
// Use this UIImagePickerController's delegate method
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
// Getting image user just has shoot
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Fixing to stick with only one orientation (UIImageOrientationUp in this case)
switch (image.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
image = [UIImage imageWithCGImage:image.CGImage
scale:image.scale
orientation:UIImageOrientationUp]; // change this if you need another orientation
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
// The image is already in correct orientation
break;
}
// Do whatever you want with image (likely pass it to some UIImageView to display)
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
// Dismiss UIImagePickerController
[picker dismissModalViewControllerAnimated:NO];
}
注意:UIImageView 读取imageOrientation 属性并相应地显示UIImage。
【讨论】:
UIImagePickerController 上启用横向的 iOS 7 时代问题(对我来说这是开箱即用的),但没有人解释为什么我在 横向右侧 拍摄的照片显示向上- 在 UIImageView 上向下,而我在 landscape left 中拍摄的那些则没有。谢谢你的回答。