3

谁能告诉我如何使用 UIImagePickerController(相机和相册)代表确定图像的方向,相应地旋转,并将其分配给 UIImageView?

4

3 回答 3

14

Vodkhang 的回答部分不正确——现代相机在拍摄照片时会将明确的方向放入图像中。这已经很常见了大约 5 年多。

我以前使用此答案中的代码通过直接从图片中读取“方向”信息来进行旋转:

UIImagePickerController 相机预览是横向应用程序中的纵向

于 2011-02-21T11:38:33.270 回答
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相应显示。

于 2013-10-28T17:13:26.803 回答
-4

不, UIImagePickerController 不能明确地做到这一点。

如果要根据图像的内容来确定。这是一个难题。如何在不了解图像内容的情况下确定图像的方向?

我可以建议您检查返回图像的宽度和长度并查看它是纵向还是横向的技巧。为了旋转图像,外面有一些库和代码,但我没有尝试太多。你可以看看这个博客

于 2010-09-16T16:34:43.070 回答