如何以正确的方向旋转图像

时间:2019-02-14 09:46:20

标签: ios swift cgimage

我正在捕获图像并将其以NSData格式存储。当我以纵向设备方向捕获图像时,图像向左旋转90度。当我以横向设备的方向捕获图像时,我以适当的方式获取图像。我尝试下面的功能。但我不知道cgImage需要用作CGImage。

func rotateImageProperly(image:UIImage) -> UIImage {

    let normalImage:UIImage!

    if(image.imageOrientation == .up) {
        normalImage = image
    }else if (image.imageOrientation == .left){
        normalImage = UIImage(cgImage: <#T##CGImage#>, scale: 1.0, orientation: UIImageOrientation.right)
    }


    return normalImage
}

1 个答案:

答案 0 :(得分:0)

使用以下代码正确旋转图像:

func imageOrientation(_ src:UIImage)->UIImage {
    if src.imageOrientation == UIImageOrientation.up {
        return src
    }
    var transform: CGAffineTransform = CGAffineTransform.identity
    switch src.imageOrientation {
    case UIImageOrientation.down, UIImageOrientation.downMirrored:
        transform = transform.translatedBy(x: src.size.width, y: src.size.height)
        transform = transform.rotated(by: CGFloat(Double.pi))
        break
    case UIImageOrientation.left, UIImageOrientation.leftMirrored:
        transform = transform.translatedBy(x: src.size.width, y: 0)
        transform = transform.rotated(by: CGFloat(Double.pi))
        break
    case UIImageOrientation.right, UIImageOrientation.rightMirrored:
        transform = transform.translatedBy(x: 0, y: src.size.height)
        transform = transform.rotated(by: CGFloat(Double.pi))
        break
    case UIImageOrientation.up, UIImageOrientation.upMirrored:
        break
    }

    switch src.imageOrientation {
    case UIImageOrientation.upMirrored, UIImageOrientation.downMirrored:
        transform.translatedBy(x: src.size.width, y: 0)
        transform.scaledBy(x: -1, y: 1)
        break
    case UIImageOrientation.leftMirrored, UIImageOrientation.rightMirrored:
        transform.translatedBy(x: src.size.height, y: 0)
        transform.scaledBy(x: -1, y: 1)
    case UIImageOrientation.up, UIImageOrientation.down, UIImageOrientation.left, UIImageOrientation.right:
        break
    }

    let ctx:CGContext = CGContext(data: nil, width: Int(src.size.width), height: Int(src.size.height), bitsPerComponent: (src.cgImage)!.bitsPerComponent, bytesPerRow: 0, space: (src.cgImage)!.colorSpace!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!

    ctx.concatenate(transform)

    switch src.imageOrientation {
    case UIImageOrientation.left, UIImageOrientation.leftMirrored, UIImageOrientation.right, UIImageOrientation.rightMirrored:
        ctx.draw(src.cgImage!, in: CGRect(x: 0, y: 0, width: src.size.height, height: src.size.width))
        break
    default:
        ctx.draw(src.cgImage!, in: CGRect(x: 0, y: 0, width: src.size.width, height: src.size.height))
        break
    }

    let cgimg:CGImage = ctx.makeImage()!
    let img:UIImage = UIImage(cgImage: cgimg)

    return img
}

用法:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
    var image = info[UIImagePickerControllerOriginalImage] as! UIImage
    image = self.imageOrientation(image)
}