如何压缩缩小图像的大小,然后上传到Parse作为PFFile? (迅速)

如何压缩缩小图像的大小,然后上传到Parse作为PFFile? (迅速),第1张

概述我直接在手机上拍照后尝试上传一个图像文件到Parse。但它抛出一个例外: Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘PFFile cannot be larger than 10485760 bytes’ 这里是我的代码: 在第一个视图控制器: override func pre 我直接在手机上拍照后尝试上传一个图像文件到Parse。但它抛出一个例外:

Terminating app due to uncaught exception ‘NSinvalidargumentexception’,reason: ‘PFfile cannot be larger than 10485760 bytes’

这里是我的代码:

在第一个视图控制器:

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。有没有办法减少图像的大小或分辨率?

是的,您可以使用UIImageJPEGRepresentation而不是UIImagePNGRepresentation来减少您的图像文件大小。您可以只创建一个扩展UIImage如下:

Xcode 8.2•Swift 3.0.2

extension 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 PNG 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 PNG 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.    var png: Data? { return UIImagePNGRepresentation(self) }    /// 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)    }}

用法:

if let imageData = image.jpeg(.lowest) {    print(imageData.count)}

Swift 2.3

extension UIImage {    var uncompressedPNGData: NSData?      { return UIImagePNGRepresentation(self)        }    var highestQualityJPEGNSData: NSData? { return UIImageJPEGRepresentation(self,1.0)  }    var highQualityJPEGNSData: NSData?    { return UIImageJPEGRepresentation(self,0.75) }    var mediumQualityJPEGNSData: NSData?  { return UIImageJPEGRepresentation(self,0.5)  }    var lowQualityJPEGNSData: NSData?     { return UIImageJPEGRepresentation(self,0.25) }    var lowestQualityJPEGNSData:NSData?   { return UIImageJPEGRepresentation(self,0.0)  }}

然后你可以这样使用它:

if let imageData = image.lowestQualityJPEGNSData {    print(imageData.length)}
总结

以上是内存溢出为你收集整理的如何压缩缩小图像的大小,然后上传到Parse作为PFFile? (迅速)全部内容,希望文章能够帮你解决如何压缩缩小图像的大小,然后上传到Parse作为PFFile? (迅速)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1056039.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存