UIImage方向Swift [英] UIImage Orientation Swift

查看:466
本文介绍了UIImage方向Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已编写此代码以使用Swift中的AVFoundation库捕获图像:

I have written this code to capture an image using the AVFoundation library in Swift:

@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度。

It works fine, but when I go to the photo library the image shows rotated 90 degrees counter clockwise.

有人可以给我一个关于在哪里挖掘修复的提示这个?

Can someone give me an hint on where to dig to fix this?

推荐答案

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

You should be using a slightly different writeImage method:

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

(1) get the orientation from the UIImage imageOrientation property (an enum), and cast it to ALAssetOrientation (an enum with the same Int values as UIImageOrientation)

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

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

(2) use a similar-but-different method on ALAssetLibrary

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

这适用于我在Objective-C中的 ...我已经快速转到了Swift(如上所述)但我收到了编译器警告。

This works for me in Objective-C ... I have had a quick go at translating to Swift (as above) but I am getting compiler warnings.


无法使用类型'(CGImage!,orientation:ALAssetOrientation,completionBlock:NilLiteralConvertible)'的参数列表调用'writeImageToSavedPhotosAlbum'

Cannot invoke 'writeImageToSavedPhotosAlbum' with an argument list of type '(CGImage!, orientation: ALAssetOrientation, completionBlock: NilLiteralConvertible)'

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

Perhaps you could try (I don't have the time to construct a full AVFoundation pipeline in Swift to test this definitively)

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

If you can't get that to work, the other solution is to extract the exif metadata from the sampleBuffer and pass it through to the method you are already using

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

这篇关于UIImage方向Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆