简体   繁体   English

减少png图像的文件大小

[英]Reduce the file size of png image

How can I reduce the file size of png image to less than 64KB and without losing the transparent background and maintain 512 X 512 pixels in Swift. 如何在不丢失透明背景的情况下将png图像的文件大小减小到64KB以下,并在Swift中保持512 X 512像素。

I have tried this code: 我已经试过这段代码:

   func resizeImageOriginalSize(targetSize: CGSize) -> UIImage {
    var actualHeight: Float = Float(self.size.height)
    var actualWidth: Float = Float(self.size.width)
    let maxHeight: Float = Float(targetSize.height)
    let maxWidth: Float = Float(targetSize.width)
    var imgRatio: Float = actualWidth / actualHeight
    let maxRatio: Float = maxWidth / maxHeight
    var compressionQuality: Float = 0.5
    //50 percent compression

    if actualHeight > maxHeight || actualWidth > maxWidth {
        if imgRatio < maxRatio {
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight
            actualWidth = imgRatio * actualWidth
            actualHeight = maxHeight
        }
        else if imgRatio > maxRatio {
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth
            actualHeight = imgRatio * actualHeight
            actualWidth = maxWidth
        }
        else {
            actualHeight = maxHeight
            actualWidth = maxWidth
            compressionQuality = 0.4
        }
    }
    let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth), height: CGFloat(actualHeight))
    UIGraphicsBeginImageContextWithOptions(rect.size, false, CGFloat(compressionQuality))
    self.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()



    UIGraphicsEndImageContext()
    return newImage!
}

but size is not what I expected. 但是大小不是我所期望的。

You should check WWDC Session 219 - Image and graphics best practices . 您应该查看WWDC会议219-图像和图形最佳实践 First third of it contains useful information on how to optimize graphics, particularly memory footprint. 它的前三分之一包含有关如何优化图形的有用信息,尤其是内存占用量。

One of the ways proposed is the Downsampling technique. 提出的方法之一是降采样技术。 Here is a snippet: 这是一个片段:

func downsample(imageAt imageURL: URL, to pointSize: CGSize, scale: CGFloat) -> UIImage {
    let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
    let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, imageSourceOptions)!
    let maxDimensionInPixels = max(pointSize.width, pointSize.height) * scale
    let downsampleOptions =
            [kCGImageSourceCreateThumbnailFromImageAlways: true,
            kCGImageSourceShouldCacheImmediately: true,
            kCGImageSourceCreateThumbnailWithTransform: true,
            kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels] as CFDictionary
    let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions)!
    return UIImage(cgImage: downsampledImage)
}

Downsampling will reduce image memory size proportionally to the downsampled size change. 下采样将与下采样的大小变化成比例地减小图像存储器的大小。 Further there is always a tradeoff between image quality and memory footprint and the result highly depends on compression algorithm and compression settings. 此外,在图像质量和内存占用量之间总是要权衡取舍,其结果高度取决于压缩算法和压缩设置。

This stackoverflow thread , and especially this answer provides some tips. 这个stackoverflow线程 ,尤其是这个答案提供了一些技巧。

Hope it helps. 希望能帮助到你。

我正在压缩到WebP formate,与创建PNG相比,它提供的文件大小比PNG小3倍左右,因为我要创建同时也接受WebP的WhatsApp贴纸包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM