Resizing image to fit UIImageView Resizing image to fit UIImageView ios ios

Resizing image to fit UIImageView


Hi I would try this...

- (void)changeImages{    UIImage *img11 = [UIImage imageNamed@"dog.jpeg"];    u11.contentMode = UIViewContentModeScaleAspectFit;    u11.clipsToBounds = YES;    [u11 setImage:img11];}- (void)viewWillAppear:animated{    [super viewWillAppear:animated];    [self changeImages];}

This will scale the image (up or down) so that it fits inside the imageView. Having clipsToBounds isn't necessary but will stop the image from displaying outside the frame of your imageView.

HTH.


Add to your UIViewController.m:

-(UIImage *)resizeImage:(UIImage *)image imageSize:(CGSize)size{    UIGraphicsBeginImageContext(size);    [image drawInRect:CGRectMake(0,0,size.width,size.height)];    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();    // here is the scaled image which has been changed to the size specified    UIGraphicsEndImageContext();    return newImage;}

Using:

UIImage *image = [UIImage imageNamed:@"image.png"];CGSize size = CGSizeMake(50, 63); // set the width and height UIImage *resizedImage = [self resizeImage:image imageSize:size];

I hope it helps.


CGSize size=CGSizeMake(79, 84);//set the width and heightUIGraphicsBeginImageContext(size);[image drawInRect:CGRectMake(0,0,size.width,size.height)];UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();//here is the scaled image which has been changed to the size specifiedUIGraphicsEndImageContext();

This works for sure and don't forget to import QuartzCore FrameWork..

Have a Happy Coding (^_^)....


matomo