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

时间:2017-03-11 00:04:54

标签: ios swift swift3 uiimageview uiimage

我正在使用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
}

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

enter image description here

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

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

结果是:

enter image description here

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

1 个答案:

答案 0 :(得分: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)

结果:

enter image description here

这应该让你对程序有一个大概的了解。