Shradha Shradha - 2 months ago 6x
iOS Question

Fitting a UIImage in UIImageView

I need to resize my

according to the size of
. My image is too small, so i need to scale it up. I was not able to do it using:

self.firstImage.contentMode = UIViewContentModeScaleAspectFit;

Please help.


You just need to set the frame of your UIImageView and set the contentMode to one of the resizing options.

Or you can use this utility method, if you actually need to resize an image:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    return newImage;

Or look at these links :