UIImagePickerController 图像方向

发布于 2024-09-19 04:37:39 字数 82 浏览 10 评论 0原文

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

Can anyone tell me how to use the UIImagePickerController (camera and album) delegates determine the image's orientation, rotate accordingly, and assign it to a UIImageView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明月松间行 2024-09-26 04:37:39

沃德康的答案部分不正确——现代相机在拍照时会在图像中加入明确的方向。这种情况已经常见了大约 5 年多了。

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

UIImagePickerController 相机预览在横向应用程序中为纵向

Vodkhang's answer is partially incorrect - modern cameras put an explicit orientation into the image when they take the photo. This has been common for about 5+ years now.

I've previously used the code in this answer to do the rotation by reading the "orientation" info direct from the picture:

UIImagePickerController camera preview is portrait in landscape app

落叶缤纷 2024-09-26 04:37:39
    // 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

    // 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];
    }

Note: UIImageView reads the imageOrientation property and displays UIImage accordingly.

枯寂 2024-09-26 04:37:39

不,UIImagePickerController 无法显式执行此操作。

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

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

No, UIImagePickerController cannot do it explicitly.

If you want to determine based on image's content. It is kind of hard problem. How can you determine an image's orientation without understanding the content inside of it?

I can suggest you a trick that you examine the width and the length of the returned image and see if it is portrait or landscape. For rotating the image, there are some libraries and codes outside but I didn't try them much. You can take a look at this blog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文