Swift 3 - 旋转图像在 ImageView 中无法正确显示

Posted

技术标签:

【中文标题】Swift 3 - 旋转图像在 ImageView 中无法正确显示【英文标题】:Swift 3 - Rotated image doesn't show up correctly in ImageView 【发布时间】:2017-03-11 00:04:53 【问题描述】:

我正在使用 Swift 3,我正在 iPhone 7 上开发。

基于对如何旋转图像的大量搜索,我正在使用以下代码:

func sFunc_imageFixOrientation(img:UIImage) -> UIImage 


    // No-op if the orientation is already correct
    if (img.imageOrientation == UIImageOrientation.up) 
        return img;
    
    // 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 = CGAffineTransform.identity

    if (img.imageOrientation == UIImageOrientation.left
        || img.imageOrientation == UIImageOrientation.leftMirrored) 
        transform = transform.translatedBy(x: img.size.width, y: 0)
        transform = transform.rotated(by: CGFloat(M_PI_2))
    

    if (img.imageOrientation == UIImageOrientation.right
        || img.imageOrientation == UIImageOrientation.rightMirrored) 
        transform = transform.translatedBy(x: 0, y: img.size.height);
        transform = transform.rotated(by: CGFloat(-M_PI_2));
    

    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    let ctx:CGContext = CGContext(data: nil, width: Int(img.size.width), height: Int(img.size.height),
                                  bitsPerComponent: img.cgImage!.bitsPerComponent, bytesPerRow: 0,
                                  space: img.cgImage!.colorSpace!,
                                  bitmapInfo: img.cgImage!.bitmapInfo.rawValue)!

    ctx.concatenate(transform)


    if (img.imageOrientation == UIImageOrientation.left
        || img.imageOrientation == UIImageOrientation.leftMirrored
        || img.imageOrientation == UIImageOrientation.right
        || img.imageOrientation == UIImageOrientation.rightMirrored
        ) 

        ctx.draw(img.cgImage!, in: CGRect(x:0,y:0,width:img.size.height,height:img.size.width))

     else 
        ctx.draw(img.cgImage!, in: CGRect(x:0,y:0,width:img.size.width,height:img.size.height))
    


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

    return imgEnd

在我的应用程序中,我正在使用纵向模式为以下一张纸拍照。

我希望图片以横向显示在我的UIImageView 中,所以我使用了上面的功能:

let rotatedImage = sFunc_imageFixOrientation(img : stillPicture.image!)     
tempImageShow.contentMode = UIViewContentMode.scaleAspectFit  
tempImageShow.image = rotatedImage

结果是:

如您所见,图像根本没有旋转。它在右侧填充了一个黑色矩形并被挤压以填充空间。如何让实际图像旋转,上面的代码有什么问题?

【问题讨论】:

你的代码什么都不做。第一个测试通过:if (img.imageOrientation == UIImageOrientation.up) return img; ,我们完成了。 嗯,我在里面放了一些打印语句,它似乎在 UIImageOrientation.right 块中。 嗯。好吧,那我不知道。 :( 问题是你所做的与我所做的完全不同。我会使用 UIGraphicsImageContext。 我从另一个 *** 答案中获取了这段代码,该答案是关于旋转 UIImage 的,所以如果你有其他方法,我会全力以赴。 【参考方案1】:

我真的不想涵盖所有情况,但只是为了您的图像,将其旋转为横向显示,以便书写正确,我编写了以下代码:

let im = UIImage(named:"picture")!
let r = UIGraphicsImageRenderer(size: 
    CGSize(width: im.size.height, height: im.size.width))
let outim = r.image  _ in
    let con = UIGraphicsGetCurrentContext()!
    con.translateBy(x: 0, y: im.size.width)
    con.rotate(by: -.pi/2)
    im.draw(at: .zero)
 
 let iv = UIImageView(image:outim)
 self.view.addSubview(iv)

结果:

这应该可以让您大致了解该过程。

【讨论】:

哇,这对我有用,马上!我试图花一些时间来了解发生了什么以及为什么我的旧代码根本不起作用!哇,非常感谢!如果可以的话 +100。 有关讨论,请参阅apeth.com/iosBook/ch15.html#_graphics_context_transforms。从那时起,事情并没有发生根本性的变化,即使我们现在使用 Swift,并且 iOS 10 中也出现了 UIGraphicsImageRenderer。它与您所做的并没有太大的不同,但我发现使用图像图形上下文最容易。 简化了代码;恐怕我第一次想的不是很清楚。 :) @matt 我正在使用这种方法在将图像粘贴到图像视图之前调整图像: image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];但它不会在我的图像视图中正确定位(它没有镜像)。这应该工作还是我必须像这个答案所示的图形上下文自己转换它? @EthanG 请提出您的问题作为问题。无需在这里“偷窃”cmets。

以上是关于Swift 3 - 旋转图像在 ImageView 中无法正确显示的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIPanGestureRecognizer- Swift 3 旋转 ImageView

iOS Swift 检测图像旋转

Swift 2.0 Xcode 7.1 图像选择器前置摄像头图像在选择时旋转

如何在swift 3中使用imageview嵌套标签

ScrollView 和 ImageView - 多次设备旋转后图像未居中

Android:将imageview中的图像旋转一个角度