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? (迅速)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)