如何将UIImage保存到Parse

时间:2016-01-07 23:52:10

标签: swift parse-platform uiimage

我拍摄了一张图片并将其转换为jpeg格式

let jpgImage = UIImageJPEGRepresentation(image1, 0.5)

接下来我想将此图像上传到Parse作为PFUser的对象部分。当我尝试这个时,我得到错误

[Error]: The object is too large -- should be less than 128 kB

我不知道如何修复此错误,只需注册用户即可。感谢您提供任何帮助或建议!

1 个答案:

答案 0 :(得分:0)

错误表示对象限制为128kb,如果您的图像大于此值,您可以选择使用下面的函数缩小它。

func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size
    let widthRatio  = targetSize.width  / image.size.width
    let heightRatio = targetSize.height / image.size.height
    // Figure out what our orientation is, and use that to form the rectangle
    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
    } else {
        newSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRectMake(0, 0, newSize.width, newSize.height)

    // Actually do the resizing to the rect using the ImageContext stuff
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.drawInRect(rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

使用上述功能,您可以将图像调整为动态 维度。以上代码调整大小200 * 200的图像。通过调用 低于功能。

self.ResizeImage(UIImage(named: "yourImageName")!, targetSize: CGSizeMake(200.0, 200.0)) 

来自here的源代码。

首先通过以下方式检查图像大小:

let sizeOfImage = image?.size

if let image = image {
    let sizeOfImage = image.size
    // Check the size here and resize if needed
}