ios - 减少png图像的文件大小

标签 ios swift uiimage core-image ios-stickers

如何在不丢失透明背景的情况下将 png 图像的文件大小减小到小于 64KB,并在 Swift 中保持 512 X 512 像素。

我试过这段代码:

   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!
}

但尺寸不是我预期的。

最佳答案

你应该检查WWDC Session 219 - Image and graphics best practices .它的前三分之一包含有关如何优化图形的有用信息,尤其是内存占用。

建议的方法之一是下采样技术。这是一个片段:

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)
}

下采样将根据下采样大小的变化按比例减少图像内存大小。此外,图像质量和内存占用之间始终存在权衡,结果在很大程度上取决于压缩算法和压缩设置。

stackoverflow thread ,尤其是 this answer提供了一些提示。

希望对您有所帮助。

关于ios - 减少png图像的文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53780468/

相关文章:

ios - 如何检查某个对象是否存在于 NSMutableArray 的某个索引处?

ios - 创建 UIImageView 时上下文无效

swift - 无法单击 iOS 14 中 UITableViewCell 内的按钮

ios - 按钮图像看起来颜色相反

ios - 从较大的 UIImage 中裁剪一部分 UIImage,并包括非图像部分

ios - 错误 : Assigning to 'CLLocationCoordinate2D' from incompatible type 'id'

ios - 这个 dispatch_once 单例可以返回 nil 吗?

android - 智能手机设备的唯一设备标识符

ios - userNotificationCenter didReceive 响应包含空通知(watchOS)

ios - 类型剪贴板在 swift 中没有成员 'objects'