在 swift 中捕获图像后,图像的方向在 ipad 中发生了变化

Posted

技术标签:

【中文标题】在 swift 中捕获图像后,图像的方向在 ipad 中发生了变化【英文标题】:orientation of image is changed in ipad after capture image in swift 【发布时间】:2019-04-02 11:59:16 【问题描述】:

我想在从相机捕获的图像上添加标记图像,然后快速保存到照片库,但是图像在照片库中以错误的方向保存,所以请建议我将如何解决这个问题。 我想在 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

                        )
                    
                
            
        )
    

【问题讨论】:

看到这篇文章***.com/questions/10600613/…,github.com/onevcat/Kingfisher/issues/87 【参考方案1】:

我使用下面的代码来获取图像的方向并转换为所需的方向。看看能不能用这个。

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

【讨论】:

以上是关于在 swift 中捕获图像后,图像的方向在 ipad 中发生了变化的主要内容,如果未能解决你的问题,请参考以下文章

Objective-c:如何在启用纵向锁定的情况下在捕获后修复图像方向

捕获显示横向方向的 UIimage 视图中存储的图像

在 Swift 中捕获原始图像类型

使用 swift 3 在 iOS 中存储捕获的图像

是否可以在 Swift 中捕获的图像中添加自己的元数据

Cocoa:在 Swift 中保存时捕获屏幕并缩放图像