快速捕获图像后,在ipad中更改图像的方向

时间:2019-04-02 11:59:17

标签: ios swift iphone ipad

我想在相机拍摄的图像上添加标记图像,然后快速保存到照片库中,但是图像以错误的方向保存在照片库中,因此请提出如何解决此问题的建议。 我想以iPhone的pot-rate模式和iPad的lanscape模式保存图像。所以请指出我错了。 我的代码在那里: {

    if let videoConnection = stillImageOutput.connection(with: AVMediaType.video) {

        stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (CMSampleBuffer, Error) in
            if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(CMSampleBuffer!) {

                if let cameraImage = UIImage(data: imageData) {



                    if let  img2 = self.imgWaterMark.image {



                       // let rect = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)

                        let rect = CGRect(x: 0, y: 0, width: (self.previewLayer?.frame.size.width)! , height: (self.previewLayer?.frame.size.height)!)

                        UIGraphicsBeginImageContextWithOptions((self.previewLayer?.frame.size)!, true, 180)
                        let context = UIGraphicsGetCurrentContext()

                        context?.setFillColor(UIColor.white.cgColor)
                        context?.fill(rect)

                        cameraImage.draw(in: rect, blendMode: .normal, alpha: 1)
                        img2.draw(in:self.imgWaterMark.frame , blendMode: .normal, alpha: 1)
                        let result = UIGraphicsGetImageFromCurrentImageContext()
                        UIGraphicsEndImageContext()

                       // let img = result

                        //let finalimage = self.rotateCameraImageToProperOrientation(imageSource: cameraImage)
                        UIImageWriteToSavedPhotosAlbum(result ?? cameraImage, nil, nil, nil)
                       // self.textToImage(drawText:self.lblTitle!.text! as NSString, inImage: result ?? cameraImage, atPoint: CGPoint(x: 0, y: 110))

                        AppSharedData.sharedInstance.showAlert(title: "", message: "Image saved", viewController: self, actions: ["OK"], callBack: { (action) in

                        })
                    }
                }
            }
        })
    }
}

1 个答案:

答案 0 :(得分:0)

我使用下面的代码来获取Image的方向并将其转换为所需的方向。请查看是否可以使用。

img_Photo.image = self.imageOrientation((img_Photo?.image)!)


//*******************************
func imageOrientation(_ src:UIImage)->UIImage {
    print(src.imageOrientation.rawValue)
    print(src.imageOrientation.hashValue)
    if src.imageOrientation == UIImageOrientation.up {
        return src
    }/*else if src.imageOrientation == UIImageOrientation.down {
        return src
    }else if src.imageOrientation == UIImageOrientation.left {
        return src
    }else if src.imageOrientation == UIImageOrientation.right {
        return src
    }*/

    var transform: CGAffineTransform = CGAffineTransform.identity
    switch src.imageOrientation {
    case UIImageOrientation.up, UIImageOrientation.upMirrored:
        transform = transform.translatedBy(x: src.size.width, y: src.size.height)
        transform = transform.rotated(by: CGFloat(M_PI))
        break
    case UIImageOrientation.down, UIImageOrientation.downMirrored:
        transform = transform.translatedBy(x: src.size.width, y: src.size.height)
        transform = transform.rotated(by: CGFloat(M_PI))
        break
    case UIImageOrientation.left, UIImageOrientation.leftMirrored:
        transform = transform.translatedBy(x: src.size.width, y: 0)
        transform = transform.rotated(by: CGFloat(M_PI_2))
        break
    case UIImageOrientation.right, UIImageOrientation.rightMirrored:
        transform = transform.translatedBy(x: 0, y: src.size.height)
        transform = transform.rotated(by: CGFloat(-M_PI_2))
        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
}