Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift ios reduce image size before upload [duplicate]

Tags:

ios

swift

I am trying to reduce the file size of my images as much as possible before I upload them. Right now I am doing this:

if let firstImageData = UIImageJPEGRepresentation(pickedImage, 0.1) {  
                                        self.imgArray.append(firstImageData)
}

This will take any image coming from the camera or photo album, make it jpg and reduce its size.

I have set the setting to 0.1 but when I upload my images the size still end up around 300-350kb, is there any way I could resize them even more, I am aiming towards 50-70kb

like image 903
user2722667 Avatar asked Apr 06 '17 13:04

user2722667


People also ask

How do I resize the UIImage to reduce upload image size?

It reduces the image size to less than 100kb. It works proportionally! extension UIImage { class func scaleImageWithDivisor(img: UIImage, divisor: CGFloat) -> UIImage { let size = CGSize(width: img. size.

How do I compress an image in SwiftUI?

Create an extension Reduce the image height so it becomes smaller and uses less space. Create a new file called Extensions. swift and import SwiftUI at the top. Then, create an extension for UIImage, called aspectFittedToHeight, which will take a newHeight and return a smaller UIImage.


2 Answers

you can resize your image first to smaller size using these extension by resizing it by percent or width

extension UIImage {
    func resizeWithPercent(percentage: CGFloat) -> UIImage? {
        let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: size.width * percentage, height: size.height * percentage)))
        imageView.contentMode = .ScaleAspectFit
        imageView.image = self
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        imageView.layer.renderInContext(context)
        guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
        UIGraphicsEndImageContext()
        return result
    }
    func resizeWithWidth(width: CGFloat) -> UIImage? {
        let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))))
        imageView.contentMode = .ScaleAspectFit
        imageView.image = self
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        imageView.layer.renderInContext(context)
        guard let result = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
        UIGraphicsEndImageContext()
        return result
    }
}

to use it just call it like this

myImage = myImage.resizeWithWidth(700)!

Now next you can still compress it using compression ratio of your choice

let compressData = UIImageJPEGRepresentation(myImage, 0.5) //max value is 1.0 and minimum is 0.0
let compressedImage = UIImage(data: compressData!)
like image 169
Jagdeep Avatar answered Dec 04 '22 17:12

Jagdeep


You only can change size of image (less size = less data) and compress it by using image compression algorithms like JPEG, there is no other way (better algorythm = less size in same quality).

I heard google improved JPEG algorithm using neural networks lately (Google’s TensorFlow)

like image 42
Robert Juz Avatar answered Dec 04 '22 17:12

Robert Juz