从相机捕获照片后,我正在对图像进行压缩(400kb和1 Mb),在iPhone 6中看起来差不多3秒,而在iPhone 6s中不到1秒。 无需手动压缩,是否可以获取缩略图和原始图像?
用于图像压缩的代码
extension UIImage {
// MARK: - UIImage+Resize
func compressTo(_ expectedSizeInMb:Int) -> Data? {
let sizeInBytes = expectedSizeInMb * 1024 * 1024
var needCompress:Bool = true
var imgData:Data?
var compressingValue:CGFloat = 1.0
while (needCompress && compressingValue > 0.0) {
if let data:Data = jpegData(compressionQuality: compressingValue) {
if data.count < sizeInBytes {
needCompress = false
imgData = data
} else {
compressingValue -= 0.1
}
}
}
if let data = imgData {
if (data.count < sizeInBytes) {
return data
}
}
return nil
}
}
if let imageData = image.compressTo(1) {
print(imageData)
}
答案 0 :(得分:0)
对于保存在照片库中的图像:
尝试:
return [
'title' => 'required|max:512',
'content' => 'required',
'tag' => 'nullable'
];
对于从相机捕获的图像,您可以获取图像像素而无需重新计算数据计数:
let phAsset = info[UIImagePickerController.InfoKey.phAsset] as! PHAsset
let options = PHImageRequestOptions()
options.deliveryMode = .fastFormat
options.isSynchronous = false
// you can change your target size to CGSize(width: Int , height: Int) any number you want.
PHImageManager.default().requestImage(for: phAsset, targetSize: PHImageManagerMaximumSize, contentMode: .default, options: options, resultHandler: { image , _ in
let thumbnail = image
// use your thumbnail
})