从 UIImagePickerController 获取图像后,UIImageView 为 iPhone 5 旋转图像

Posted

技术标签:

【中文标题】从 UIImagePickerController 获取图像后,UIImageView 为 iPhone 5 旋转图像【英文标题】:After getting image from UIImagePickerController, UIImageView rotates image for iPhone 5 【发布时间】:2016-10-31 04:46:42 【问题描述】:

我正在使用 UIImagePickerController 使用相机捕捉图像。我的应用支持的方向是纵向。我看到 iPhone 5 的奇怪行为。我正在使用 Xcode 7 和 swift 2.0。 iPhone 5 操作系统版本为 8.4。我的应用的部署目标是 8.0。

问题是。 1. 对于 iPhone 5,拍摄图像后,图像会以拍摄图像的相应模式显示。但是在我按下“使用照片”标准选项后,当图像显示在 UIImageView 上时,图像会自动向左旋转。不知道为什么。如果我从照片库中选择图像,图像不会旋转。我不想旋转图像。 我看到了类似的帖子,有更好的解释和实际图像,但没有得到回答。 UIImageView rotates image with retina 4 iPhone simulator but not retina 3.5/regular simulator 我尝试了这篇帖子中几乎所有的快速解决方案:ios UIImagePickerController result image orientation after upload 和其他帖子,但似乎没有任何效果。我使用了 shouldAutorotate()、sFunc_imageFixOrientation(),并从这篇文章中添加了扩展。

    另外,对于这两款设备,在按下“使用照片”选项后,上传图片大约需要 10 秒。能不能做得更快。

这是我的代码:

func openCamera()

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) 
        dispatch_async(dispatch_get_main_queue(), 
            let imagePicker = UIImagePickerController();
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
            imagePicker.allowsEditing = false;
            imagePicker.delegate = self;
            imagePicker.modalPresentationStyle = .FormSheet
            self.presentViewController(imagePicker, animated: true, completion: nil);
        );

    


func openGallary() 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) 
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        imagePicker.allowsEditing = true
        self.presentViewController(imagePicker, animated: true, completion: nil)
    


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) 
    profileImage?.image =   image
    self.dismissViewControllerAnimated(true, completion: nil);

【问题讨论】:

【参考方案1】:

这是我观察到的。当您使用相机拍摄图像时,图像会逆时针旋转 90 度,并且图像方向设置为 UIImageOrientation.Up,因此函数 sFunc_imageFixOrientation iOS UIImagePickerController result image orientation after upload 中的代码将不起作用。如果您从照片库中选择图像,则图像将按原样显示。

修改功能:

    func sFunc_imageFixOrientation(img:UIImage) -> UIImage 

        // We need to calculate the proper transformation to make the image upright.
        // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
        var transform:CGAffineTransform = CGAffineTransformIdentity
        // Rotate image clockwise by 90 degree
        if (img.imageOrientation == UIImageOrientation.Up) 
            transform = CGAffineTransformTranslate(transform, 0, img.size.height);
            transform = CGAffineTransformRotate(transform,  CGFloat(-M_PI_2));
        

        if (img.imageOrientation == UIImageOrientation.Down
            || img.imageOrientation == UIImageOrientation.DownMirrored) 

            transform = CGAffineTransformTranslate(transform, img.size.width, img.size.height)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
        

        if (img.imageOrientation == UIImageOrientation.Left
            || img.imageOrientation == UIImageOrientation.LeftMirrored) 

            transform = CGAffineTransformTranslate(transform, img.size.width, 0)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
        

        if (img.imageOrientation == UIImageOrientation.Right
            || img.imageOrientation == UIImageOrientation.RightMirrored) 

            transform = CGAffineTransformTranslate(transform, 0, img.size.height);
            transform = CGAffineTransformRotate(transform,  CGFloat(-M_PI_2));
        

        if (img.imageOrientation == UIImageOrientation.UpMirrored
            || img.imageOrientation == UIImageOrientation.DownMirrored) 

            transform = CGAffineTransformTranslate(transform, img.size.width, 0)
            transform = CGAffineTransformScale(transform, -1, 1)
        

        if (img.imageOrientation == UIImageOrientation.LeftMirrored
            || img.imageOrientation == UIImageOrientation.RightMirrored) 

            transform = CGAffineTransformTranslate(transform, img.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
        


        // Now we draw the underlying CGImage into a new context, applying the transform
        // calculated above.
        let ctx:CGContextRef = CGBitmapContextCreate(nil, Int(img.size.width), Int(img.size.height),
                                                     CGImageGetBitsPerComponent(img.CGImage), 0,
                                                     CGImageGetColorSpace(img.CGImage),
                                                     CGImageGetBitmapInfo(img.CGImage).rawValue)!;
        CGContextConcatCTM(ctx, transform)


        if (img.imageOrientation == UIImageOrientation.Left
            || img.imageOrientation == UIImageOrientation.LeftMirrored
            || img.imageOrientation == UIImageOrientation.Right
            || img.imageOrientation == UIImageOrientation.RightMirrored
            || img.imageOrientation == UIImageOrientation.Up
            ) 

            CGContextDrawImage(ctx, CGRectMake(0,0,img.size.height,img.size.width), img.CGImage)
         else 
            CGContextDrawImage(ctx, CGRectMake(0,0,img.size.width,img.size.height), img.CGImage)
        


        // And now we just create a new UIImage from the drawing context
        let cgimg:CGImageRef = CGBitmapContextCreateImage(ctx)!
        let imgEnd:UIImage = UIImage(CGImage: cgimg)

        return imgEnd
    

仅当从相机捕获图像时才调用此函数。我知道我的答案并不完美,但如果没有什么适合您的情况,您绝对可以尝试。

【讨论】:

以上是关于从 UIImagePickerController 获取图像后,UIImageView 为 iPhone 5 旋转图像的主要内容,如果未能解决你的问题,请参考以下文章

从 UITabBarController 驱动 UIImagePickerController

从 UIImagePickerController 保存视频

UIImagePickerController 从 UICollectionViewCell 内的 UIImageView 呈现

如何从 UIImagePickerController 获取图像文件名

从 UIImagePickerController 获取图像位置

从 UIImagePickerController 传递图像数据以进行后台处理