iphone - Compress Large UIImage -
so im doing compressing image repeatedly until fits within size frame. it takes many attempts, give , later tell user image large.
my issue after image size has been reduced, size goes way again when changed uiimage.
here code reducing image size:
double compressionratio = 1; int resizeattempts = 5; nsdata * imgdata = uiimagejpegrepresentation([info objectforkey:@"uiimagepickercontrolleroriginalimage"],compressionratio); nslog(@"starting size: %i", [imgdata length]); //trying push below around 0.4 meg while ([imgdata length] > 400000 && resizeattempts > 0) { resizeattempts -= 1; nslog(@"image bigger 400000 bytes. resizing."); nslog(@"%i attempts remaining",resizeattempts); //increase compression amount compressionratio = compressionratio*0.5; //test size before compression nslog(@"current size: %i",[imgdata length]); imgdata = uiimagejpegrepresentation([info objectforkey:@"uiimagepickercontrolleroriginalimage"],compressionratio); //test size after compression nslog(@"new size: %i",[imgdata length]); } //set image comprssed version savedimage = [uiimage imagewithdata:imgdata]; //check how big image been compressed , put uiimageview nsdata *enddata = uiimagejpegrepresentation(savedimage,1.0); nslog(@"ending size: %i", [enddata length]);
ok here console report:
starting image size: 4076994 image bigger 400000 bytes. resizing. 4 attempts remaining current size: 4076994 new compressed size: 844482 image bigger 400000 bytes. resizing. 3 attempts remaining current size: 844482 new compressed size: 357459 ending image size: 2090332
as can see image started @ 4mb big. compressed 350kb, in end made uiimage 2mb.
thanks in advance can make sense of this.
i have modified function bit , got result, try it, reduce 10mb image 3mb.
-(void)compraseimage { uiimage *largeimage = [info objectforkey:@"uiimagepickercontrolleroriginalimage"]; double compressionratio = 1; int resizeattempts = 5; nsdata * imgdata = uiimagejpegrepresentation(largeimage,compressionratio); nslog(@"starting size: %i", [imgdata length]); //trying push below around 0.4 meg while ([imgdata length] > 400000 && resizeattempts > 0) { resizeattempts -= 1; nslog(@"image bigger 400000 bytes. resizing."); nslog(@"%i attempts remaining",resizeattempts); //increase compression amount compressionratio = compressionratio*0.5; nslog(@"compressionratio %f",compressionratio); //test size before compression nslog(@"current size: %i",[imgdata length]); imgdata = uiimagejpegrepresentation(largeimage,compressionratio); //test size after compression nslog(@"new size: %i",[imgdata length]); } //set image comprssed version savedimage = [uiimage imagewithdata:imgdata]; //check how big image been compressed , put uiimageview // *** made change here, again storing highest resolution *** nsdata *enddata = uiimagejpegrepresentation(largeimage,compressionratio); nslog(@"ending size: %i", [enddata length]); nsstring *path = [self createpath:@"myimage.jpg"]; nslog(@"%@",path); [enddata writetofile:path atomically:yes]; } -(nsstring *) createpath:(nsstring *)withfilename { nsarray *paths =nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *path = [documentsdirectory stringbyappendingpathcomponent:withfilename]; return path; }
Comments
Post a Comment