views:

192

answers:

2

Hi,

so currently i'm trying to crop and resize a picture to fit it into a specific size without loosing the ratio.

a small image to show what i mean:

alt text

i played a bit with vocaro's categories but they don't work with png's and have problems with gifs. also the image doesn't get cropped.

does anyone have a suggestion how to do this resizing the best way or probably have a link to an existing library/categorie/whatever?

thanks for all hints!

p.s.: does ios implement a "select a excerpt" so that i have the right ratio and only have to scale it?!

A: 

The resizing code on my site does work with PNGs, just not ones with alpha layers.

When you say, "the image doesn't get cropped", what exactly do you mean?

vocaro
i mean resizing this(http://cl.ly/6755178bc5f09124388d) using this method( https://gist.github.com/39516037fbd1ed0d21c9 ) (100x85) results that(http://cl.ly/bed90faa4628f696b640). you can also try my sample code(http://cl.ly/afe22b2d3837b9be1ff1). also try the png in this sample code. no alpha and not working
choise
You are using UIViewContentModeScaleAspectFill. Perhaps UIViewContentModeScaleAspectFit is what you really want?
vocaro
tried both, nothing works. you can use my sample code. just check the image that gets written into the camera roll. =/
choise
A: 

This method will do what you want and is a category of UIImage for ease of use. I went with resize then crop, you could switch the code around easily enough if you want crop then resize. The bounds checking in the function is purely illustrative. You might want to do something different, for example center the crop rect relative to the outputImage dimensions but this ought to get you close enough to make whatever other changes you need.

@implementation UIImage( resizeAndCropExample )

- (UIImage *) resizeToSize:(CGSize) newSize thenCropWithRect:(CGRect) cropRect {
    CGContextRef                context;
    CGImageRef                  imageRef;
    CGSize                      inputSize;
    UIImage                     *outputImage = nil;
    CGFloat                     scaleFactor, width;

    // resize, maintaining aspect ratio:

    inputSize = self.size;
    scaleFactor = newSize.height / inputSize.height;
    width = roundf( inputSize.width * scaleFactor );

    if ( width > newSize.width ) {
        scaleFactor = newSize.width / inputSize.width;
        newSize.height = roundf( inputSize.height * scaleFactor );
    } else {
        newSize.width = width;
    }

    UIGraphicsBeginImageContext( newSize );

    context = UIGraphicsGetCurrentContext();
    CGContextDrawImage( context, CGRectMake( 0, 0, newSize.width, newSize.height ), self.CGImage );
    outputImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    inputSize = newSize;

    // constrain crop rect to legitimate bounds
    if ( cropRect.origin.x >= inputSize.width || cropRect.origin.y >= inputSize.height ) return outputImage;
    if ( cropRect.origin.x + cropRect.size.width >= inputSize.width ) cropRect.size.width = inputSize.width - cropRect.origin.x;
    if ( cropRect.origin.y + cropRect.size.height >= inputSize.height ) cropRect.size.height = inputSize.height - cropRect.origin.y;

    // crop
    if ( ( imageRef = CGImageCreateWithImageInRect( outputImage.CGImage, cropRect ) ) ) {
        outputImage = [[[UIImage alloc] initWithCGImage: imageRef] autorelease];
        CGImageRelease( imageRef );
    }

    return outputImage;
}

@end
par