CLOSE SEARCH

[Snippet] Draw Image using Quartz

– (void)drawImage:(CGContextRef)context
{

NSString *imgFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@”img.png”];
CGDataProviderRef provider = CGDataProviderCreateWithFilename([imgFileName UTF8String]);
CGImageRef imgRef = CGImageCreateWithPNGDataProvider(provider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);

//or
//UIImage *img = [UIImage imageNamed:@”img.png”];
//CGImageRef imgRef = CGImageRetain(img.CGImage);

CGContextClipToRect(context, CGRectMake(10.0, 10.0, self.bounds.size.width – 20.0, self.bounds.size.height – 20.0));
CGContextDrawTiledImage(context, CGRectMake(0, 0, CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)), imgRef);

CGContextSetLineWidth(context, 2.0);
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRect(context, CGContextGetClipBoundingBox(context));

}

Filed under: iOS