Tuesday, October 20, 2009

image cropping

This code is for cropping any image in any particular rectangle.

- (UIImage*) getSmallImageUIImage*) img
{
CGSize size = img.size;
CGFloat ratio = 0;
if (size.width < size.height) {
ratio = 36 / size.width;
} else {
ratio = 36 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

UIGraphicsBeginImageContext(rect.size);
[img drawInRect:rect];

UIImage *tempImg = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();
return [tempImg autorelease];
}

- (UIImage*)imageByCroppingUIImage *)imageToCrop toRectCGRect)rect
{

//create a context to do our clipping in
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();

//create a rect with the size we want to crop the image to
//the X and Y here are zero so we start at the beginning of our
//newly created context

CGFloat X = (imageToCrop.size.width - rect.size.width)/2;
CGFloat Y = (imageToCrop.size.height - rect.size.height)/2;


CGRect clippedRect = CGRectMake(X, Y, rect.size.width, rect.size.height);
//CGContextClipToRect( currentContext, clippedRect);



//create a rect equivalent to the full size of the image
//offset the rect by the X and Y we want to start the crop
//from in order to cut off anything before them
CGRect drawRect = CGRectMake(0,
0,
imageToCrop.size.width,
imageToCrop.size.height);

CGContextTranslateCTM(currentContext, 0.0, drawRect.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
//draw the image to our clipped context using our offset rect
//CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);


CGImageRef tmp = CGImageCreateWithImageInRect(imageToCrop.CGImage, clippedRect);

//pull the image from our cropped context
UIImage *cropped = [UIImage imageWithCGImage:tmp];//UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(tmp);
//pop the context to get back to the default
UIGraphicsEndImageContext();

//Note: this is autoreleased*/
return cropped;
}

1 comment:

  1. I'm on the fence about this, while more customization is good, I have a feeling this is a "in-progress" update, it just feels incomplete and half-way there.
    We use badge layout for apps on design approvals (visual projects), so the image being displayed is important. Old layout "feels like" it had larger images,
    maybe because the images were cropped more loosely so it's easier to tell which project it was at quick glance. Now the image is cropped closer, making it
    harder to scan thru at quick glance. I find myself needing to click into the project more often than usual. Which makes the whole user experience less
    efficient.
    I have a couple suggestions that might make it work better:
    1. Increase the height of the window the cover image is being displayed.
    2. Let us to choose which image to be displayed as "cover" (like how Pinterest handles cover images of each board, was hoping for this for a long time)
    3. Let us adjust which part of the image to show and how tight or loose the crop is (with a fixed window, let us move the image around and maybe enlarge or
    shrink it to control what shows thru the window. Pinterest does a limited form of this, which is very useful in making the cover image relevant)
    4. Allow Cover Image to be ordered in different hierarchy (currently every element can be ordered differently except the Cover Image, it seems to be stuck
    in the 2nd spot, would like the option to set it on another spot in the layout. This one seems like an easy fix, since you guys allow that for every other
    element already)

    ReplyDelete