Shradha
Shradha

Reputation: 1011

Fitting a UIImage in UIImageView

I need to resize my UIImage according to the size of UIImageView. 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.

Upvotes: 2

Views: 11204

Answers (2)

Ghulam Rasool
Ghulam Rasool

Reputation: 4064

In Swift 3.0 , it will be

func imageWithImage(image:UIImage, scaledToSize newSize:CGSize ) -> UIImage {

    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.width))
    let newImage : UIImage  = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext();
    return newImage;

}

Upvotes: 2

Camo
Camo

Reputation: 1778

try with this:

self.imageView.contentMode   = UIViewContentModeScaleAspectFill;
self.imageView.clipsToBounds = YES;

Upvotes: 0

Related Questions