有谁能告诉我如何使用UIImagePickerController(相机和相册)代表确定图像的方向,相应地旋转,并将其分配给UIImageView?
答案 0 :(得分:14)
Vodkhang的答案部分不正确 - 现代相机拍摄照片时会在图像中显示明确的方向。这已经普遍存在了大约5年多。
我之前使用过这个答案中的代码来直接从图片中读取“方向”信息来进行轮换:
UIImagePickerController camera preview is portrait in landscape app
答案 1 :(得分:2)
// 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
。
答案 2 :(得分:-4)
不,UIImagePickerController无法明确地执行此操作。
如果您想根据图片的内容确定。这是一个很难的问题。如何在不了解图像内容的情况下确定图像的方向?
我可以建议你检查返回图像的宽度和长度,看看它是纵向还是横向。为了旋转图像,外面有一些库和代码,但我没有尝试过多少。您可以查看this blog