-4

I've seen:

  • Accusations that various UIImage resizing code is wrong and has bugs
  • Resizing code which comes out with low quality images
  • Resizing code which flips the image
  • Resizing code which doesn't work correctly on portrait camera photos specifically

Is there a piece of code for resizing/cropping a UIImage that somebody could recommend?

Andrew
  • 15,935
  • 28
  • 121
  • 203
  • 1
    How about you include the code of each points such that we know what to even recommend? As of now, I have a block of code that I am happy with. I do not want to answer because I do not know what you categorized as good or bad. – Byte Jun 01 '12 at 19:09
  • 1
    It is not that bad or unreasonable a question. There is a lot of poor code/bad approaches on SO for common tasks like cropping an image. And fwiw - SO is implicitly a recommendation engine given the voting, etc. A feature I would like to see here is a way to promote and finalize solutions to a wiki where common tasks can be hashed out in one place rather than the litter of questions as there is now. – spring Jun 01 '12 at 19:12
  • @skinnyTOD That is a wonderful point and a fantastic idea. A site which states it isn't about recommendation but yet lets users vote things up and down? And the way you have to think about wording when searching, to match what people may have written, is something you shouldn't have to worry about. A wiki of knowledge off the back of questions would fit right in. – Andrew Jun 01 '12 at 19:56
  • 1
    I dont think this is actually a bad question, and indeed I have had to deal with this in the past. Here is a great site talking about this same issue. From StackOverflow: http://stackoverflow.com/questions/612131/whats-the-easiest-way-to-resize-optimize-an-image-size-with-the-iphone-sdk and http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ – trumpetlicks Jun 01 '12 at 20:24

2 Answers2

4

I use the category shown here in https://stackoverflow.com/a/10491692/1271826 which is a category that provides UIImage a series of resizing methods, which effectively supports UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFill, and UIViewContentModeScaleAspectFit.

I use this primarily for creating my thumbnail images (using my imageByScalingAspectFillSize) and do this with images of varying formats without incident.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

If you are talking about just the UIImage which doesnt contain metadata its just a matter of starting creating a graphics context and calling the uiimage draw in context method:

- (UIImage*)resizeImage:(UIImage*)jpeg withSize:(CGSize)newSize
{   
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [jpeg drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();

    return resizedImage;
}

IF what you want is to resize images that contain metadata like read from the disk or something, perform the previews method and when re-saving use the original source dictionary.

Pochi
  • 13,391
  • 3
  • 64
  • 104