In iOS application, memory management is an important aspect of making a seamless & smooth UI experience. Now to make it more challenging part comes when images are loaded. Most of the memory leaks & issues appear due to a large size of images loaded from the web interface.

So to overcome this issue it becomes necessary to resize an image before its used. While resizing we will have to make sure image will be resized to the size where actually used in the application.

Let’s say from web interface image loaded with the size of 1000 pixels. But we need to show an image on UIImageView which has size 400x400 pixels. So at the time before using actual image needs to be resized.

It will not only less the memory usage but will also improve the overall performance of the app by loading app faster.

While doing image resizing operations it becomes important to make sure image doesn’t lose its scale. If it’s ignored than image will get starched & will not look as it should be. That will result in bad programming & bad interface as well.

So here is the example for resizing the image without losing the actual scale & proportion.

Resize with new width :

This particular function requires passing actual image & new width which you wants to resize an image with & it will automatically calculate the height according to the image scale & proportion & will return a new resized image. It will not lose the actual scale of image & still, an image will be resized.

Read more: Free online mockup sites to generate eyecatchy screenshots for mobile apps

Objective C :

-(UIImage *)resizeImageWithNewWidth:(UIImage *)image newWidth:(CGFloat)nwidth {

float scale = nwidth / image.size.width;

float newHeight = image.size.height * scale;

UIGraphicsBeginImageContext(CGSizeMake(nwidth, newHeight));

[image drawInRect:CGRectMake(0, 0, nwidth, newHeight)];

UIImage *nImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return nImage;

}

Swift :

func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {

let scale = newWidth / image.size.width

let newHeight = image.size.height * scale

UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))

image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))

let newImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return newImage!

}

Resize with new height :

This particular function requires to pass actual image & new height which you wants to resize image with & it will automatically calculate the width according the image scale & proportion & will return new resized image. It will not loose the actual scale of image & still image will be resized.

Objective C :

-(UIImage *)resizeImageWithNewHeight:(UIImage *)image newheight:(CGFloat)nheight {

float scale = nheight / image.size.height;

float newwidth = image.size.width * scale;

UIGraphicsBeginImageContext(CGSizeMake(newwidth, nheight));

[image drawInRect:CGRectMake(0, 0, newwidth, nheight)];

UIImage *nImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return nImage;

}

Swift :

func resizeImage(image: UIImage, newHeight: CGFloat) -> UIImage {

let scale = newHeight / image.size.height

let newWidth = image.size.width * scale

UIGraphicsBeginImageContext(CGSize(width:newWidth, height:newHeight))

image.draw(in: CGRect(x:0, y:0, width:newWidth, height:newHeight))

let newImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return newImage!

}

Share
Share