我正在创建一个相机应用程序,一切都很好,但是上传到Firebase的照片的文件大小太大了。他们是在3MB的类别,我希望他们是在±600±的类别。
我已经将AVCapturePhotoSettings.isHighResolutionPhotoEnabled设置为false,但这似乎没有做任何事情,而且AVCapturePhotoOutput.quality.balanced只对iOS13+起作用,这在我的情况下是行不通的。
下面是向我的imageArray发送映像的委托函数
extension NewPostFromPhoto: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let error = error {
debugPrint (error)
} else {
photoData = photo.fileDataRepresentation()
UIImageWriteToSavedPhotosAlbum(UIImage(data: photoData!)!, nil, nil, nil)
self.imageArray.insert((UIImage(data: photoData!)?.fixOrientation())!, at:0)
self.recentMediaCollection.reloadData()
}
}}
下面是我的takePhoto()方法:
func takeNewPhoto () {
let settings = AVCapturePhotoSettings ()
settings.isHighResolutionPhotoEnabled = false
photoOutput?.capturePhoto(with: settings, delegate: self)
} 发布于 2019-12-12 06:29:29
使用您的代码,我得到了大约20 MB的pngData。但是当我们使用jpegData()时,我们得到了大约2.2MB。
image.jpegData(compressionQuality: 0.8)这里是来自委托的完整代码
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard let imageData = photo.cgImageRepresentation() else {
self.captureFailed(error: error?.localizedDescription ?? "Can't take image from output")
return
}
let cgimage = imageData.takeUnretainedValue()
let orientation: UIImage.Orientation = captureDevice?.position == .front ? .leftMirrored : .right
let image = UIImage(cgImage: cgimage, scale: 1, orientation: orientation)
let data = image?.jpegData(compressionQuality: 0.8)
}希望能帮上忙
https://stackoverflow.com/questions/59298092
复制相似问题