如何在Swift中从UIImage转换为HEIF/HEIC数据? [英] How can I convert from UIImage to HEIF / HEIC Data in Swift?

查看:160
本文介绍了如何在Swift中从UIImage转换为HEIF/HEIC数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSKeyedArchiver 是否适合将 UIImage 转换为 Data ?

do {
    let data = try NSKeyedArchiver.archivedData(withRootObject: UIImage(named: somePath), requiringSecureCoding: true)
    ...
} catch {
    print(error)
}

是不是矫kill过正,使用 pngData()更合适吗?

Or is it overkill and using pngData() is more appropriate?

let image = UIImage(named: somePath)
let data = image?.pngData()

以及如何从 UIImage 转换为HEIF/HEIC Data ?

and how can I convert from UIImage to HEIF / HEIC Data ?

目标是将图像保存到设备的文件系统中.

The goal is to save the image to the device's file system.

推荐答案

否.切勿使用NSKeyedArchiver将图像转换为数据.选择图像格式(HEIC,PNG,JPEG等)并获取其数据表示形式.保存图像以在UI中使用时,仅应使用PNG.大多数情况下,jpeg是首选.如果设备支持HEIC,则可以考虑图像质量和减小的数据大小.

No. Never use NSKeyedArchiver to convert your image to Data. Choose an image format (HEIC, PNG, JPEG, etc) and get its data representation. You should only use PNG when saving images to use in your UI. Most of the time jpeg is the preferred choice. If the device supports HEIC it is an option considering the image quality and reduced data size.

如果需要检查用户设备是否支持HEIC类型,可以按照以下步骤进行操作:

If you need to check if the user device supports HEIC type you can do it as follow:

var isHeicSupported: Bool {
    (CGImageDestinationCopyTypeIdentifiers() as! [String]).contains("public.heic")
}

如果需要将图像转换为HEIC,则需要从 UIImage 获取 CGImage 并转换 UIImage imageOrientation CGImagePropertyOrientation ,以在创建其数据表示形式时保留方向:

If you need to convert your image to HEIC you need to get a CGImage from your UIImage and convert your UIImage's imageOrientation to CGImagePropertyOrientation to preserve the orientation when creating its data representation:

extension UIImage {
    var heic: Data? { heic() }
    func heic(compressionQuality: CGFloat = 1) -> Data? {
        guard
            let mutableData = CFDataCreateMutable(nil, 0),
            let destination = CGImageDestinationCreateWithData(mutableData, "public.heic" as CFString, 1, nil),
            let cgImage = cgImage 
        else { return nil }
        CGImageDestinationAddImage(destination, cgImage, [kCGImageDestinationLossyCompressionQuality: compressionQuality, kCGImagePropertyOrientation: cgImageOrientation.rawValue] as CFDictionary)
        guard CGImageDestinationFinalize(destination) else { return nil }
        return mutableData as Data
    }
}


extension CGImagePropertyOrientation {
    init(_ uiOrientation: UIImage.Orientation) {
        switch uiOrientation {
            case .up: self = .up
            case .upMirrored: self = .upMirrored
            case .down: self = .down
            case .downMirrored: self = .downMirrored
            case .left: self = .left
            case .leftMirrored: self = .leftMirrored
            case .right: self = .right
            case .rightMirrored: self = .rightMirrored
        @unknown default:
            fatalError()
        }
    }
}


extension UIImage {
    var cgImageOrientation: CGImagePropertyOrientation { .init(imageOrientation) }
}


无损压缩的用途:


Usage for lossless compression:

if isHeicSupported, let heicData = image.heic {
    // write your heic image data to disk
}

或向图像添加压缩:

if isHeicSupported, let heicData = image.heic(compressionQuality: 0.75) {
    // write your compressed heic image data to disk
}

这篇关于如何在Swift中从UIImage转换为HEIF/HEIC数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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