美文网首页
Swift 使用jpegData(compressionQual

Swift 使用jpegData(compressionQual

作者: 被风吹乱的思念 | 来源:发表于2021-04-14 15:21 被阅读0次

    图像表示将占用大量的CPU和内存资源。 因此,在这种情况下,最好遵循以下几条规则:

    • 不要在主队列上运行jpegData(compressionQuality :)

    • 同时运行一个jpegData(compressionQuality :)

    错误:

    for i in 0...50 {
        DispatchQueue.global(qos: .utility).async {
            let quality = 0.02 * CGFloat(i)
            //let data = image.toJpegData(compressionQuality: quality)
            let data = image.jpegData(compressionQuality: quality)
            let size = CGFloat(data!.count)/1000.0/1024.0
            print("\(i), quality: \(quality), \(size.rounded()) mb")
        }
    }
    

    正确

    let serialQueue = DispatchQueue(label: "queue", qos: .utility, attributes: [], autoreleaseFrequency: .workItem, target: nil)
    
    for i in 0...50 {
        serialQueue.async {
            let quality = 0.02 * CGFloat(i)
            //let data = image.toJpegData(compressionQuality: quality)
            let data = image.jpegData(compressionQuality: quality)
            let size = CGFloat(data!.count)/1000.0/1024.0
            print("\(i), quality: \(quality), \(size.rounded()) mb")
        }
    }
    

    原文地址:https://www.itranslater.com/qa/details/2325693309395665920

    相关文章

      网友评论

          本文标题:Swift 使用jpegData(compressionQual

          本文链接:https://www.haomeiwen.com/subject/onaklltx.html