Home>

IOS resize images in memory

When downloading pictures from the Internet or reading pictures from albums,If the imageview itself is a fixed 300 * 200, then loading a 2000 * 2000 picture is a waste of memory.

The memory footprint of 2000 * 2000 is 2000 * 2000 * 4bit

The following two functions can be used to create a new image of a fixed size.simply put,It is core graphics to create a bitmap and then generate an image.

-(uiimage *) imagewithimage:(uiimage *) image
       scaledtosize:(cgsize) newsize;
{
  uigraphicsbeginimagecontext (newsize);
  [image drawinrect:cgrectmake (0,0, newsize.width, newsize.height)];
  uiimage * newimage=uigraphicsgetimagefromcurrentimagecontext ();
  uigraphicsendimagecontext ();
  return newimage;
}
-(uiimage *) imagewithimage:(uiimage *) image
       resizefactor:(cgfloat) scale
{
  cgsize originalsize=image.size;
  cgsize newsize=cgsizemake (originalsize.width * scale, originalsize.height * scale);
  uigraphicsbeginimagecontext (newsize);
  [image drawinrect:cgrectmake (0,0, newsize.width, newsize.height)];
  uiimage * newimage=uigraphicsgetimagefromcurrentimagecontext ();
  uigraphicsendimagecontext ();
  return newimage;
}
ios
  • Previous Example of restricting the number of controller access times with custom annotations in SpringBoot
  • Next Lucene implements multiple advanced search forms
  • Trends