tags:

views:

121

answers:

4

Hi all i am having .png image.I want to draw only half of this image.If i call method drawInRect: on image with width being half the size of image,it will still draw the full image in given rect.So how to draw half image

A: 

You could crop your existing image by using CGImageCreateWithImageInRect

nico
+1  A: 

Before calling -drawInRect, try setting a clip mask on the current context:

UIImage *image = // your image, let's say 100x100
CGRect drawingRect = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f); // adjust the origin; size has to be the image's size
CGRect clippingRect = drawingRect; // copy drawingRect...
clippingRect.size.width = 50.0f; // ...and reduce the width

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context); // save current graphics state
CGContextClipToRect(context, clippingRect);
[image drawInRect:drawingRect];
CGContextRestoreGState(context); // restore previous state (without clip mask)
Thomas Müller
A: 

If it's in a UIImageView, you can change the frame to only fit half the image, then set [imageView setClipsToBounds:YES].

Ian Henry
A: 

Clip Rect did the magic. Thanks @thomas

CGContextClipToRect(context, clippingRect);
imthi