Image Orientation Lost During Compression Swift ios -
i use following function create thumbnail of image:
func scaleimage(image: uiimage, tosize newsize: cgsize) -> (uiimage) { let newrect = cgrectintegral(cgrectmake(0,0, newsize.width, newsize.height)) uigraphicsbeginimagecontextwithoptions(newsize, false, 0) let context = uigraphicsgetcurrentcontext() cgcontextsetinterpolationquality(context, .high) let flipvertical = cgaffinetransformmake(1, 0, 0, -1, 0, newsize.height) cgcontextconcatctm(context, flipvertical) cgcontextdrawimage(context, newrect, image.cgimage) let newimage = uiimage(cgimage: cgbitmapcontextcreateimage(context)!) uigraphicsendimagecontext() return newimage }
i call in following way :
let imagethumb = self.scaleimage(currimage, tosize: cgsizemake(currimage.size.width/4, currimage.size.height/4))
i encode imagethumb using following function :
func compressimage(image:uiimage) -> nsdata { // reducing file size 10th var actualheight : cgfloat = image.size.height var actualwidth : cgfloat = image.size.width let maxheight : cgfloat = 1136.0 let maxwidth : cgfloat = 640.0 var imgratio : cgfloat = actualwidth/actualheight let maxratio : cgfloat = maxwidth/maxheight var compressionquality : cgfloat = 0.5 if (actualheight > maxheight || actualwidth > maxwidth){ if(imgratio < maxratio){ //adjust width according maxheight imgratio = maxheight / actualheight; actualwidth = imgratio * actualwidth; actualheight = maxheight; } else if(imgratio > maxratio){ //adjust height according maxwidth imgratio = maxwidth / actualwidth; actualheight = imgratio * actualheight; actualwidth = maxwidth; } else{ actualheight = maxheight; actualwidth = maxwidth; compressionquality = 1; } } let rect = cgrectmake(0.0, 0.0, actualwidth, actualheight); uigraphicsbeginimagecontext(rect.size); image.drawinrect(rect) let img = uigraphicsgetimagefromcurrentimagecontext(); let imagedata = uiimagejpegrepresentation(img, compressionquality); uigraphicsendimagecontext(); return imagedata!; }
finally, encode base64
, store in database. retrieve it, decode , display in imageview. issue photos take in portrait mode end being displayed in landscape. i'm not sure along way orientation lost.
Comments
Post a Comment