ios - NSData to UIImage quality -


i use uiimage *image = [uiimage imagewithdata: data], larger size of image 4 times.original data.length=100 kb, image has more 400 kb.you might complete compression.but i'm through following method after compression data.length=200 kb.

the following way

compression method

 // spend size (kb)     - (nsdata *)compressionimagedata:(nsdata *)data spend:(cgfloat)spend {          if (data.length < spend) {             return data;    //no compression         }         uiimage *sourceimg = [uiimage imagewithdata:data];          cgfloat compression = 0.9f;         cgfloat maxcompression = 0.1f;         nsdata *imagedata = uiimagejpegrepresentation(sourceimg, compression);          while ([imagedata length] > spend && compression > maxcompression) {             compression -= 0.1;             imagedata = uiimagejpegrepresentation(sourceimg, compression);         }         return imagedata;      } 

download image method:

- (void)loadposterimage:(nsstring *)imgurl {     weakself();     [[sdwebimagedownloader shareddownloader] downloadimagewithurl:[nsurl urlwithstring:imgurl]             options:sdwebimagedownloaderlowpriority             progress:^(nsinteger receivedsize, nsinteger expectedsize) {}             completed:^(uiimage *image, nsdata *data, nserror *error, bool finished) {                 strongself();                 //  image through [uiimage imagewithdata: data]                 self.posterimage = image;                 self.posterdata = data;              }]; } 

the image size has more 500 kb

data.lenght = 100kb

this data.length of image

https://i.stack.imgur.com/fzuvk.png

this image of image

https://i.stack.imgur.com/ozxa6.png

i dont know why, can redraw image this:

+(nsdata *)compressdatawithimage:(uiimage *)image tomaxfilesize:(nsinteger)maxfilesize { uiimage *comimage;  comimage = image; cgfloat compression = 1.0; nsdata *imagedata = uiimagejpegrepresentation(comimage, compression); nsdata *newdata; while (imagedata.length > maxfilesize) {     compression *= 0.5;     newdata = uiimagejpegrepresentation(comimage, compression);     if (newdata.length == imagedata.length) {         cgsize newsize = cgsizemake(comimage.size.width * 0.5, comimage.size.height * 0.5);         uigraphicsbeginimagecontext(newsize);         [comimage drawinrect:cgrectmake(0,0,newsize.width,newsize.height)];         comimage = uigraphicsgetimagefromcurrentimagecontext();         newdata = uiimagejpegrepresentation(comimage, compression);     }     imagedata = newdata;   }  return imagedata;  } 


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -