首页 文章

UIImagePickerController图像方向

提问于
浏览
3

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

3 回答

  • 14

    不,UIImagePickerController无法明确地执行此操作 .

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

    我可以建议你一个技巧,你检查返回图像的宽度和长度,看看它是纵向还是横向 . 为了旋转图像,外面有一些库和代码,但我没有尝试过 . 你可以看看this blog

  • 2

    Vodkhang的答案部分不正确 - 现代相机拍摄照片时会在图像中显示明确的方向 . 这已经普遍存在了大约5年 .

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

    UIImagePickerController camera preview is portrait in landscape app

  • -4
    // 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 .

相关问题