开发者

Drawing half image

Hi all i am having .pn开发者_开发知识库g 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


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)


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


You could crop your existing image by using CGImageCreateWithImageInRect


Clip Rect did the magic. Thanks @thomas

CGContextClipToRect(context, clippingRect);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜