【问题标题】:UIImage Orientation SwiftUIImage 方向 Swift
【发布时间】:2015-01-29 21:55:30
【问题描述】:

我已经编写了这段代码来使用 Swift 中的 AVFoundation 库捕获图像:

@IBAction func cameraButtonWasPressed(sender: AnyObject) {

    if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo){
        stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection){
            (imageSampleBuffer : CMSampleBuffer!, _) in

            let imageDataJpeg = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)

            var pickedImage: UIImage = UIImage(data: imageDataJpeg)!

            let library = ALAssetsLibrary()
            library.writeImageToSavedPhotosAlbum(pickedImage.CGImage,
                metadata:nil,
                completionBlock:nil)

        }


    }

}

它工作正常,但是当我转到照片库时,图像显示逆时针旋转 90 度。

有人可以告诉我在哪里可以解决这个问题吗?

【问题讨论】:

标签: ios swift uiimage avfoundation alassetslibrary


【解决方案1】:

也许这段 Swift 代码可以帮到你。

//correctlyOrientedImage.swift

import UIKit

extension UIImage {

    public func correctlyOrientedImage() -> UIImage {
        if self.imageOrientation == UIImageOrientation.Up {
            return self
        }

        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        self.drawInRect(CGRectMake(0, 0, self.size.width, self.size.height))
        var normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return normalizedImage;
    }
}

我在somewhere in stack overflow 看到了它,并把它也纳入了我的项目中。

【讨论】:

    【解决方案2】:

    您应该使用稍微不同的writeImage 方法:

    (1) 从 UIImage imageOrientation 属性(枚举)中获取方向,并将其转换为 ALAssetOrientation(与 UIImageOrientation 具有相同 Int 值的枚举)

     var orientation : ALAssetOrientation = ALAssetOrientation(rawValue:           
                                            pickedImage.imageOrientation.rawValue)!
    

    (2) 在 ALAssetLibrary 上使用相似但不同的方法

    library.writeImageToSavedPhotosAlbum(
                    pickedImage.CGImage,
                    orientation: orientation,
                    completionBlock:nil)
    

    这对我有用在 Objective-C 中 ...我已经快速翻译成 Swift(如上),但我收到了编译器警告。

    无法使用类型为“(CGImage!,方向:ALAssetOrientation,completionBlock:NilLiteralConvertible)”的参数列表调用“writeImageToSavedPhotosAlbum”

    也许你可以试试(我没有时间在 Swift 中构建完整的 AVFoundation 管道来最终测试)

    如果你不能让它工作,另一个解决方案是从 sampleBuffer 中提取 exif 元数据并将其传递给你已经在使用的方法

    library.writeImageToSavedPhotosAlbum(pickedImage.CGImage, metadata:nil, completionBlock:nil

    【讨论】:

    • 感谢@foundry 的好回答!你解决了我的问题stackoverflow.com/questions/28843045/… 如果你愿意,可以在那里回答,我会检查它是否正确。
    • @benLIVE - 对你有用 - 使用 orientation 版本或提取元数据并使用 metadata 版本?
    • 使用方向。我正在使用带有元数据的 writeImageToSavedPhotosAlbum 方法并更改为方向,就像您的示例一样。知道一种编辑元数据的方法会很好,因为不仅有方向,还有 GeoLocation 等。@foundry
    【解决方案3】:

    斯威夫特 5

    extension UIImage {
        public func correctlyOrientedImage() -> UIImage {
            if self.imageOrientation == UIImage.Orientation.up {
                return self
            }
            UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
            self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
            let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()!;
            UIGraphicsEndImageContext();
    
            return normalizedImage;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多