Created by Jamz Tang at 10 September 2011
You probably aware of the method CGImageCreateWithImageInRect
that's already defined in the Foundation framework. It is absolute fine for developers familiar well with CoreGraphics and doesn’t mind manually taking care of the image orientations.
We loved UIKit anyway.
Use this UIImage-JTImageCrop
category instead.
Now you use this instead of the CoreGraphics method.
+ (UIImage *)imageWithImage:(UIImage *)image cropInRect:(CGRect)rect;
Last but not least, you’d somehow want to further abstract it with a proportional rect value. (Imagine you are defining a cropping area on the screen and want to crop a full sized image, you’d transform the visible area to the full sized photo). A handly method is also created for this purpose.
+ (UIImage *)imageWithImage:(UIImage *)image cropInRelativeRect:(CGRect)rect;
Enjoy the snippet!
Clone this repository:
git clone git://gist.github.com/6912933.git UIImage-JTImageCrop
If you think this is useful, share this article with your friends :)
blog comments powered by DisqusUINibDecoderProxy Observes what's encoded in an NSCoder object
JTKeyValueObserver Revisiting KVO+Block, the simplest version.
MethodSwizzle Method Swizzling in Objective-C
UITableViewDeleteActionResponder Quick hack to enable delete menu item in UITableView menuController
UIApplicationAddition Quickly switch supported UIInterfaceOrientation for your View Controllers
JTTargetActionBlock Adding Block support for UIControl's Target-Action mechanism
NSArray-JTArraySplit Splitting an array to several components
UIImage+JTImageDecode Force decompressing UIImage in background to achieve better performance
UINavigationBar-JTDropShadow Adding drop shadow on UINavigationBar (before iOS 6)
UIImage-JTImageCrop Crop an image in specific rect
UIView+JTRemoveAnimated Adding fadeout effect to any -[UIViews removeFromSuperview]
JTStringAddition NSStringf. Simpler printf styled +[NSString stringWithFormat:]
UIView-JTViewToImage Rendering any UIViews into UIImage in one line (updated with iOS 7 support)
NSObject-JTNibLoader Loading a Nib file programmatically using NSObject category
UIImage-JTColor Creating a placeholder UIImage dynamically with color
NSObject-JTCancelableScheduledBlock Cancelable Scheduled Blocks in Objective-C