代码之家  ›  专栏  ›  技术社区  ›  Bryan

UIImagePickerController图像方向

  •  3
  • Bryan  · 技术社区  · 14 年前

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

    3 回复  |  直到 14 年前
        1
  •  -4
  •   vodkhang    14 年前

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

        2
  •  14
  •   Community CDub    8 年前

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

    UIImagePickerController camera preview is portrait in landscape app

        3
  •  2
  •   Yevhen Dubinin rednuht    11 年前
        // 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 相应地。