-1

I've created a UIImageView which contains an image with a high resolution. The problem is that the resolution of that image is too high to put into the imageView. The size of the imageView is 92 x 91 (so it's small). It contains an UIImage, whose resolution is too high so it looks ugly in the UIImageView. So how can I reduce the resolution of that UIImage?

My code for the UIImageView:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:pngFilePath]];
    myImageView.frame = CGRectMake(212.0, 27, 92,91);
user2420219
  • 33
  • 1
  • 3
  • What is your definition of resolution? It's often used as the number of pixels per unit. It sounds like you define it as the dimensions of an image. – El Tomato May 25 '13 at 13:26

4 Answers4

2

have a look at this

https://stackoverflow.com/a/2658801

This will help you to resize your image according to your need

Add method to your code and call like this

UIImage *myImage =[UIImage imageWithContentsOfFile:pngFilePath];
UIImage *newImage =[UIImage imageWithImage:myImage scaledToSize:CGSizeMake(92,91)];
Community
  • 1
  • 1
George
  • 191
  • 4
1

You can resize an image using this method that returns a resized image :

 -(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    // Here pass new size you need
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
0

try with this

     myImageView.contentMode = UIViewContentModeScaleToFill;
SAMIR RATHOD
  • 3,512
  • 1
  • 20
  • 45
0

You need to downsize the image, which will reduce the resolution, make it easier to store. I actually wrote a class (building on some stuff from SO) that does just that. It's on my github, take a look:

https://github.com/pavlovonline/UIImageResizer

the main method is

-(UIImage*)resizeImage:(UIImage*)image toSize:(CGFloat)size

so you give this method the size to which you want to downsize your image. If the height is greater than the width, it will auto-calculate the middle and give you a perfectly centered square. Same for width being greater than height. If you need an image that is not square, make your own adjustments.

so you'll get back a downsized UIImage which you can then put into your UIImageView. Save some memory too.

Live2Enjoy7
  • 1,075
  • 2
  • 11
  • 22