swift ios reduce image size before upload -


this question has answer here:

i trying reduce file size of images as possible before upload them. right doing this:

if let firstimagedata = uiimagejpegrepresentation(pickedimage, 0.1) {                                           self.imgarray.append(firstimagedata) } 

this take image coming camera or photo album, make jpg , reduce size.

i have set setting 0.1 when upload images size still end around 300-350kb, there way resize them more, aiming towards 50-70kb

you can resize image first smaller size using these extension resizing 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 call

myimage = myimage.resizewithwidth(700)! 

now next can still compress using compression ratio of choice

let compressdata = uiimagejpegrepresentation(myimage, 0.5) //max value 1.0 , minimum 0.0 let compressedimage = uiimage(data: compressdata!) 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -