在以PFFile格式上传到Parse之前,如何压缩缩小图像的大小?(迅速)

直接在手机上拍照后,我试图将图像文件上传到Parse。但这会引发异常:


由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ PFFile不能大于10485760字节”


这是我的代码:


在第一视图控制器中:


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "getImage")

    {

        var svc = segue.destinationViewController as! ClothesDetail

        svc.imagePassed = imageView.image

    }

}

在上传图像的视图控制器中:


let imageData = UIImagePNGRepresentation(imagePassed)

let imageFile = PFFile(name: "\(picName).png", data: imageData)


var userpic = PFObject(className:"UserPic")

userpic["picImage"] = imageFile`

但是我仍然需要将照片上传到Parse。有什么方法可以减小图像的尺寸或分辨率?


HUH函数
浏览 572回答 3
3回答

冉冉说

是的,你可以使用UIImageJPEGRepresentation,而不是UIImagePNGRepresentation减少图像文件的大小。您可以按照以下方式创建扩展UIImage:Xcode 8.2•Swift 3.0.2extension UIImage {    enum JPEGQuality: CGFloat {        case lowest  = 0        case low     = 0.25        case medium  = 0.5        case high    = 0.75        case highest = 1    }    /// Returns the data for the specified image in JPEG format.    /// If the image object’s underlying image data has been purged, calling this function forces that data to be reloaded into memory.    /// - returns: A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.    func jpeg(_ quality: JPEGQuality) -> Data? {        return UIImageJPEGRepresentation(self, quality.rawValue)    }}编辑/更新:Xcode 10 Swift 4.2extension UIImage {    enum JPEGQuality: CGFloat {        case lowest  = 0        case low     = 0.25        case medium  = 0.5        case high    = 0.75        case highest = 1    }    /// Returns the data for the specified image in JPEG format.    /// If the image object’s underlying image data has been purged, calling this function forces that data to be reloaded into memory.    /// - returns: A data object containing the JPEG data, or nil if there was a problem generating the data. This function may return nil if the image has no data or if the underlying CGImageRef contains data in an unsupported bitmap format.    func jpeg(_ jpegQuality: JPEGQuality) -> Data? {        return jpegData(compressionQuality: jpegQuality.rawValue)    }}用法:if let imageData = image.jpeg(.lowest) {    print(imageData.count)}

慕桂英4014372

如果要将图像大小限制为某个具体值,可以执行以下操作:import UIKitextension UIImage {&nbsp; &nbsp; // MARK: - UIImage+Resize&nbsp; &nbsp; func compressTo(_ expectedSizeInMb:Int) -> UIImage? {&nbsp; &nbsp; &nbsp; &nbsp; let sizeInBytes = expectedSizeInMb * 1024 * 1024&nbsp; &nbsp; &nbsp; &nbsp; var needCompress:Bool = true&nbsp; &nbsp; &nbsp; &nbsp; var imgData:Data?&nbsp; &nbsp; &nbsp; &nbsp; var compressingValue:CGFloat = 1.0&nbsp; &nbsp; &nbsp; &nbsp; while (needCompress && compressingValue > 0.0) {&nbsp; &nbsp; &nbsp; &nbsp; if let data:Data = UIImageJPEGRepresentation(self, compressingValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if data.count < sizeInBytes {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; needCompress = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgData = data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; compressingValue -= 0.1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if let data = imgData {&nbsp; &nbsp; &nbsp; &nbsp; if (data.count < sizeInBytes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return UIImage(data: data)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp;}

千万里不及你

Jus Fixing for Xcode 7,于09/21/2015测试,工作正常:只需创建一个扩展UIImage,如下所示:extension UIImage{&nbsp; &nbsp; var highestQualityJPEGNSData: NSData { return UIImageJPEGRepresentation(self, 1.0)! }&nbsp; &nbsp; var highQualityJPEGNSData: NSData&nbsp; &nbsp; { return UIImageJPEGRepresentation(self, 0.75)!}&nbsp; &nbsp; var mediumQualityJPEGNSData: NSData&nbsp; { return UIImageJPEGRepresentation(self, 0.5)! }&nbsp; &nbsp; var lowQualityJPEGNSData: NSData&nbsp; &nbsp; &nbsp;{ return UIImageJPEGRepresentation(self, 0.25)!}&nbsp; &nbsp; var lowestQualityJPEGNSData: NSData&nbsp; { return UIImageJPEGRepresentation(self, 0.0)! }}然后,您可以像这样使用它:let imageData = imagePassed.lowestQualityJPEGNSData
打开App,查看更多内容
随时随地看视频慕课网APP