6

I need help resizing a UIImage.

For example: I'm displaying a lot images in a UICollection View, but the size of those images is 2 to 4 MB. I need compress or resize those images.

I found this: How to compress/resize image on iPhone OS SDK before uploading to a server? but I don't understand how to implement it.

Community
  • 1
  • 1
user2868048
  • 81
  • 1
  • 2
  • 4
  • 2
    It would help if you provide some code showing what you've tried so far. – Connor Pearson Dec 04 '13 at 23:43
  • 1
    If you're addressing memory usage when your collection view has many images open at the same time, then you want to focus on resizing, not compression (because compression affects the persistent memory storage, not the memory usage). This is a resizing routine that I use: http://stackoverflow.com/a/10491692/1271826 – Rob Dec 05 '13 at 12:42
  • the accepted answer does match the title but not the questions text ... – Daij-Djan Mar 08 '14 at 22:56

4 Answers4

21

Not quite sure if you want to resize or compress or both.

Below is the code for just compression :

Use JPEG Compression in two simple steps:

1) Convert UIImage to NSData

UIImage *rainyImage =[UImage imageNamed:@"rainy.jpg"];
NSData *imgData= UIImageJPEGRepresentation(rainyImage,0.1 /*compressionQuality*/);

this is lossy compression and image size is reduced.

2) Convert back to UIImage;

UIImage *image=[UIImage imageWithData:imgData];

For scaling you can use answer provided by Matteo Gobbi. But scaling might not be a the best alternative. You would rather prefer to have a thumbnail of the actual image by compression because scaling might make look your image bad on a retina display device.

Bannings
  • 10,376
  • 7
  • 44
  • 54
Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
3

I wrote this function to scale an image:

- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {
    CGSize actSize = image.size;
    float scale = actSize.width/actSize.height;

    if (scale < 1) {
        newSize.height = newSize.width/scale;
    } else {
        newSize.width = newSize.height*scale;
    }


    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

The use is easy, for example:

[self scaleImage:yourUIImage toSize:CGMakeSize(300,300)];
Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
3
lowResImage = [UIImage imageWithData:UIImageJPEGRepresentation(highResImage, quality)];
SahilS
  • 396
  • 5
  • 7
user3358463
  • 125
  • 15
0
 -(UIImage *) resizeImage:(UIImage *)orginalImage resizeSize:(CGSize)size
 {
CGFloat actualHeight = orginalImage.size.height;
CGFloat actualWidth = orginalImage.size.width;

float oldRatio = actualWidth/actualHeight;
float newRatio = size.width/size.height;
if(oldRatio < newRatio)
{
    oldRatio = size.height/actualHeight;
    actualWidth = oldRatio * actualWidth;
    actualHeight = size.height;
}
else
{
    oldRatio = size.width/actualWidth;
    actualHeight = oldRatio * actualHeight;
    actualWidth = size.width;
}

CGRect rect = CGRectMake(0.0,0.0,actualWidth,actualHeight);
UIGraphicsBeginImageContext(rect.size);
[orginalImage drawInRect:rect];
orginalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return orginalImage;
 }
      //this image you can add it to imageview.....  
mahesh chowdary
  • 432
  • 5
  • 13