iOS图片缓存和高斯模糊


    private var bgImageGaussDataDict:[Int:Data] = [:]

        var guassImageData = bgImageGaussDataDict[gauss]
        if guassImageData == nil {
            let image = bgImageOrigin?.sd_blurredImage(withRadius: CGFloat(gauss))
            // 缓存经过压缩的jpeg data而不是UIImage,内存占用非常小
            guassImageData = image?.jpeg(.low)
            bgImageGaussDataDict[gauss] = guassImageData
        }

        if let data = guassImageData {
            self.bgImageV.image = UIImage(data: data)
        }


    enum JPEGQuality: CGFloat {
        case lowest  = 0
        case low     = 0.25
        case medium  = 0.5
        case high    = 0.75
        case highest = 1
    }

    /// Returns the data for the specified image in JPEG format.
    /// If the image object’s underlying image data has been purged, calling this function forces that data to be reloaded into memory.
    /// - returns: A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.
    func jpeg(_ jpegQuality: JPEGQuality) -> Data? {
        return jpegData(compressionQuality: jpegQuality.rawValue)
    }
posted @ 2022-07-18 17:44  rome753  阅读(114)  评论(0编辑  收藏  举报