Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PNG数据转换为JPG数据,反之亦然,无需在Swift中使用UIKit_Swift - Fatal编程技术网

将PNG数据转换为JPG数据,反之亦然,无需在Swift中使用UIKit

将PNG数据转换为JPG数据,反之亦然,无需在Swift中使用UIKit,swift,Swift,我想知道是否有可能获得PNG数据并将其转换为JPG数据而不使用UIKit。诸如此类: enum Format { case png case jpg } func convert(data: Data, to format: Format) -> Data? { switch format { case .png: return data.toJPG case .jpg: return data.toPNG } 谢

我想知道是否有可能获得PNG数据并将其转换为JPG数据而不使用UIKit。诸如此类:

enum Format {
    case png
    case jpg
}

func convert(data: Data, to format: Format) -> Data? {
    switch format {
    case .png:
        return data.toJPG
    case .jpg:
        return data.toPNG
}

谢谢

您可以加载jpeg/png数据并从中初始化CGImage,使用它创建jpeg或png目标图像。比如:





您可以查看ImageMagick库:您想做什么,在iOS以外的其他操作系统上运行代码?我想拥有UIKit免费类,这样它就可以放在我的核心模块上(干净的代码)我还看到,如果我在将JPG转换为PNG时使用UIImage方法,当JPG仅为2MB@ReimondHill考虑到PNG没有压缩选项,这是意料之中的。工作起来很有魅力!这就是我想要遵循的方法
extension Data {
    var pngFromJPEG: Data? {
        guard
            let dataProvider = CGDataProvider(data: self as CFData),
            let cgImage = CGImage(jpegDataProviderSource: dataProvider, decode: nil, shouldInterpolate: false, intent: .defaultIntent),
            let mutableData = CFDataCreateMutable(nil, 0),
            let destination = CGImageDestinationCreateWithData(mutableData, "public.png" as CFString, 1, nil)
        else { return nil }
        CGImageDestinationAddImage(destination, cgImage,nil)
        guard CGImageDestinationFinalize(destination) else { return nil }
        return mutableData as Data
    }
    func jpegFromPNG(compressionQuality: CGFloat = 1) -> Data? {
        guard
            let dataProvider = CGDataProvider(data: self as CFData),
            let cgImage = CGImage(pngDataProviderSource: dataProvider, decode: nil, shouldInterpolate: false, intent: .defaultIntent),
            let mutableData = CFDataCreateMutable(nil, 0),
            let destination = CGImageDestinationCreateWithData(mutableData, "public.jpeg" as CFString, 1, nil)
        else { return nil }
        CGImageDestinationAddImage(destination, cgImage,[kCGImageDestinationLossyCompressionQuality: compressionQuality] as CFDictionary)
        guard CGImageDestinationFinalize(destination) else { return nil }
        return mutableData as Data
    }
}
let profileURL = Bundle.main.url(forResource: "image", withExtension: "jpg")!
let jpegData = try! Data(contentsOf: profileURL)

if let pngData = jpegData.pngFromJPEG {
    let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    
    do {
        let destPNG = docs.appendingPathComponent("test.png")
        try pngData.write(to: destPNG)
        if let jpegData = pngData.jpegFromPNG() {
            let destJPEG = docs.appendingPathComponent("test.jpeg")
            try jpegData.write(to: destJPEG)
        }
    } catch {
        print(error)
    }

}