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

如何调整UIImage的大小以减少上传图像大小

如何调整UIImage的大小以减少上传图像大小

PIPIONE 2019-08-30 17:14:48
我一直在搜索谷歌,并且只会遇到减少高度/宽度的库或者通过CoreImage编辑UIImage外观的库。但我没有看到或找到一个库,帖子解释了如何减少图像大小,所以当它上传时,它不是完整的图像大小。到目前为止我有这个:        if image != nil {        //let data = NSData(data: UIImagePNGRepresentation(image))        let data = UIImagePNGRepresentation(image)        body.appendString("--\(boundary)\r\n")        body.appendString("Content-Disposition: form-data; name=\"image\"; filename=\"randomName\"\r\n")        body.appendString("Content-Type: image/png\r\n\r\n")        body.appendData(data)        body.appendString("\r\n")    }它正在发送12MB的照片。我怎样才能减少到1mb?谢谢!
查看完整描述

3 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

这是我按照调整图像大小的方式。


 -(UIImage *)resizeImage:(UIImage *)image

{

   float actualHeight = image.size.height;

   float actualWidth = image.size.width;

   float maxHeight = 300.0;

   float maxWidth = 400.0;

   float imgRatio = actualWidth/actualHeight;

   float maxRatio = maxWidth/maxHeight;

   float compressionQuality = 0.5;//50 percent compression


   if (actualHeight > maxHeight || actualWidth > maxWidth)

   {

    if(imgRatio < maxRatio)

    {

        //adjust width according to maxHeight

        imgRatio = maxHeight / actualHeight;

        actualWidth = imgRatio * actualWidth;

        actualHeight = maxHeight;

    }

    else if(imgRatio > maxRatio)

    {

        //adjust height according to maxWidth

        imgRatio = maxWidth / actualWidth;

        actualHeight = imgRatio * actualHeight;

        actualWidth = maxWidth;

    }

    else

    {

        actualHeight = maxHeight;

        actualWidth = maxWidth;

    }

   }


   CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);

   UIGraphicsBeginImageContext(rect.size);

   [image drawInRect:rect];

   UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

   NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);

   UIGraphicsEndImageContext();


   return [UIImage imageWithData:imageData];


}

使用此方法,我的6.5 MB的图像减少到104 KB。


Swift 4代码:


func resize(_ image: UIImage) -> UIImage {

    var actualHeight = Float(image.size.height)

    var actualWidth = Float(image.size.width)

    let maxHeight: Float = 300.0

    let maxWidth: Float = 400.0

    var imgRatio: Float = actualWidth / actualHeight

    let maxRatio: Float = maxWidth / maxHeight

    let compressionQuality: Float = 0.5

    //50 percent compression

    if actualHeight > maxHeight || actualWidth > maxWidth {

        if imgRatio < maxRatio {

            //adjust width according to maxHeight

            imgRatio = maxHeight / actualHeight

            actualWidth = imgRatio * actualWidth

            actualHeight = maxHeight

        }

        else if imgRatio > maxRatio {

            //adjust height according to maxWidth

            imgRatio = maxWidth / actualWidth

            actualHeight = imgRatio * actualHeight

            actualWidth = maxWidth

        }

        else {

            actualHeight = maxHeight

            actualWidth = maxWidth

        }

    }

    let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth), height: CGFloat(actualHeight))

    UIGraphicsBeginImageContext(rect.size)

    image.draw(in: rect)

    let img = UIGraphicsGetImageFromCurrentImageContext()

    let imageData = img?.jpegData(compressionQuality: CGFloat(compressionQuality)) 

    UIGraphicsEndImageContext()

    return UIImage(data: imageData!) ?? UIImage()

}


查看完整回答
反对 回复 2019-08-30
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

如果有人想要使用Swift 3和4将图像大小调整为小于1MB。


只需复制并粘贴此扩展程序:


extension UIImage {


func resized(withPercentage percentage: CGFloat) -> UIImage? {

    let canvasSize = CGSize(width: size.width * percentage, height: size.height * percentage)

    UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)

    defer { UIGraphicsEndImageContext() }

    draw(in: CGRect(origin: .zero, size: canvasSize))

    return UIGraphicsGetImageFromCurrentImageContext()

}


func resizedTo1MB() -> UIImage? {

    guard let imageData = UIImagePNGRepresentation(self) else { return nil }


    var resizingImage = self

    var imageSizeKB = Double(imageData.count) / 1000.0 // ! Or devide for 1024 if you need KB but not kB


    while imageSizeKB > 1000 { // ! Or use 1024 if you need KB but not kB

        guard let resizedImage = resizingImage.resized(withPercentage: 0.9),

            let imageData = UIImagePNGRepresentation(resizedImage)

            else { return nil }


        resizingImage = resizedImage

        imageSizeKB = Double(imageData.count) / 1000.0 // ! Or devide for 1024 if you need KB but not kB

    }


    return resizingImage

}

}

并使用:


let resizedImage = originalImage.resizedTo1MB()

编辑: 请注意它是阻止UI,所以如果您认为这是适合您案例的正确方法,请转到后台主题。


查看完整回答
反对 回复 2019-08-30
  • 3 回答
  • 0 关注
  • 695 浏览

添加回答

举报

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