0

我有一个应用程序,我想从现有的 UIImages 创建缩略图。我使用下面的函数获取全尺寸 UIImage 的参数并返回 100pxl 图标/拇指 UIImage。这一切似乎都很好,但我注意到一些生成的图标/拇指 UIImages 与原始图像相比旋转了 90 度。下面的代码中是否存在问题,我没有看到导致某些图像旋转而不是其他图像?也许是从 UIImages 创建拇指的更好解决方案?

func returnThumbnail(bigImage: UIImage) -> UIImage {
    var thumbnail = UIImage()

    if let imageData = UIImagePNGRepresentation(bigImage){
        let options = [
            kCGImageSourceCreateThumbnailWithTransform: true,
            kCGImageSourceCreateThumbnailFromImageAlways: true,
            kCGImageSourceThumbnailMaxPixelSize: 100] as CFDictionary // Specify your desired size at kCGImageSourceThumbnailMaxPixelSize. 100 seems standard

        imageData.withUnsafeBytes { ptr in
            guard let bytes = ptr.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
                return
            }
            if let cfData = CFDataCreate(kCFAllocatorDefault, bytes, imageData.count){
                let source = CGImageSourceCreateWithData(cfData, nil)!
                let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options)!
                thumbnail = UIImage(cgImage: imageReference) // You get your thumbail here
            }
        }
    }
    return thumbnail
}
4

0 回答 0