为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUH函数 2019-10-15 15:44:27
直接在手机上拍照后,我试图将图像文件上传到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。有什么方法可以减小图像的尺寸或分辨率?
查看完整描述

3 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

是的,你可以使用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 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.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 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)

}


查看完整回答
反对 回复 2019-10-15
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

如果要将图像大小限制为某个具体值,可以执行以下操作:


import UIKit


extension UIImage {

    // MARK: - UIImage+Resize

    func compressTo(_ expectedSizeInMb:Int) -> UIImage? {

        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 = UIImageJPEGRepresentation(self, compressingValue) {

            if data.count < sizeInBytes {

                needCompress = false

                imgData = data

            } else {

                compressingValue -= 0.1

            }

        }

    }


    if let data = imgData {

        if (data.count < sizeInBytes) {

            return UIImage(data: data)

        }

    }

        return nil

    } 

}


查看完整回答
反对 回复 2019-10-15
?
千万里不及你

TA贡献1784条经验 获得超9个赞

Jus Fixing for Xcode 7,于09/21/2015测试,工作正常:


只需创建一个扩展UIImage,如下所示:


extension UIImage

{

    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)! }

}

然后,您可以像这样使用它:


let imageData = imagePassed.lowestQualityJPEGNSData


查看完整回答
反对 回复 2019-10-15
  • 3 回答
  • 0 关注
  • 611 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信