如何通过压缩函数

时间:2015-10-09 03:09:34

标签: swift parse-platform compression

我想拥有它,以便数组中包含的多个UIImages可以被压缩然后通过压缩函数运行但是我无法弄清楚如何让函数允许通过它传递一组UIImages 。任何帮助将非常感激。这是功能。

 func compressForUpload(original:UIImage, withHeightLimit heightLimit:CGFloat, andWidthLimit widthLimit:CGFloat)->UIImage{



    let originalSize = original.size
    var newSize = originalSize

    if originalSize.width > widthLimit && originalSize.width > originalSize.height {

        newSize.width = widthLimit
        newSize.height = originalSize.height*(widthLimit/originalSize.width)

    } else if originalSize.height > heightLimit && originalSize.height > originalSize.width {

        newSize.height = heightLimit
        newSize.width = originalSize.width*(heightLimit/originalSize.height)
    }

    UIGraphicsBeginImageContext(newSize)
    original.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    let compressedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return compressedImage
}

以下是将数组发送到函数的代码。

let compressedImage = self.compressForUpload(self.selectedImages, withHeightLimit: 720, andWidthLimit: 1080)
                            let imageData = UIImagePNGRepresentation(compressedImage)
                            let imageFile = PFFile(name: "image.png", data: imageData!)

                            post["ImageFile"] = imageFile

1 个答案:

答案 0 :(得分:0)

创建一个包装函数,它接受并返回一个图像数组。在内部,该函数使用循环内的现有函数。最好将这些功能保持逻辑分离,而不是将它们混合在一起(尽管完全可能并不是可重用的)。