ios - Swift Code for reducing / compressing pictures saved to parse -
please @ code below if believe there can done improve swift code please let me know - converted code objective-c off raw engineering website www.rawend.com
@ibaction func signup(sender: anyobject) {
let imagedata = uiimagepngrepresentation(self.profilepic.image) let image = uiimage(data: imagedata) func compressimage(image:uiimage) -> nsdata { // reducing file size 10th var actualheight : cgfloat = image.size.height var actualwidth : cgfloat = image.size.width var maxheight : cgfloat = 1136.0 var maxwidth : cgfloat = 640.0 var imgratio : cgfloat = actualwidth/actualheight var 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; } } var rect = cgrectmake(0.0, 0.0, actualwidth, actualheight); uigraphicsbeginimagecontext(rect.size); image.drawinrect(rect) var img = uigraphicsgetimagefromcurrentimagecontext(); let imagedata = uiimagejpegrepresentation(img, compressionquality); uigraphicsendimagecontext(); return imagedata; } let compressedimage = compressimage(image!) let picture = pffile(name: "image.png", data: compressedimage) pfuser.currentuser()!.setobject(picture, forkey: "profilepic") pfuser.currentuser()!.saveinbackgroundwithblock { (success: bool, error: nserror?) -> void in if (success) { println("image has been saved") } else { println("image failed") } } }
Comments
Post a Comment