iPhone: How can you draw a piece of an image
Posted
by Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-04-19T20:43:35Z
Indexed on
2010/04/19
21:13 UTC
Read the original article
Hit count: 229
Code sample
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, self.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height), [UIImage imageNamed:@"sample.png"].CGImage);
CGContextRestoreGState(context);
}
================
I would like to copy a certain rect within an image to the context, so not the entire image is drawn but just a piece of the image. Does anyone have a solution for this? I can't find anything on google nor the documentation.
I know there are alternatives like: 1. Create a UIView with clipping and then just position the UIImageView within it. 2. Create the UIImageView within the UIScrollView and use content offset.
But I think those are lame...
© Stack Overflow or respective owner