UIImage 生成的 base64 大于它引用的(本地)base64 图像

问题描述 投票:0回答:1

我正在通过用户的相机捕获图像(使用名为 CameraManager 的库)。捕获的图像保存到文件磁盘(仅用于测试目的),并且还生成 UIImage,然后通过以下方式转换为 base64 字符串:

cameraManager.capturePictureWithCompletion({ (image, error) -> Void in

let UIImage = UIImageJPEGRepresentation(image!, 0.85)!
let base64Image = UIImage.base64EncodedString(options: .lineLength64Characters)
...

现在,当我通过图像选择器插件选择相同的图像(已保存到磁盘)时,我会返回一个(同一图像的)base64 字符串,它比原始捕获的图像小 2.5 倍多一点它所代表的。

什么可能导致额外的开销?它可能是嵌入到 UIImage 中的东西吗?我的另一个想法是将 UIImage 保存到临时文件夹,将 URI 传递给客户端,然后让客户端将其转换为 Base64 字符串,看看是否会使字符串变小。

swift base64
1个回答
0
投票

使用当前的 CameraManager 库并遇到同样的问题,我最终使用 jpeg 扩展名而不是 UIImage 编写数据。这给出了与相机库中保存的大小相同的大小。

 cameraManager.capturePictureWithCompletion({ result in
            switch result {
            case .failure: break
            case .success(let content):
                    ImageStoreService.store(image: content.asData!, forKey: createFileName(), withStorageType: .fileSystem) 
            }
        })
© www.soinside.com 2019 - 2024. All rights reserved.