show image in a CGContextRef

Posted by Ishu on Stack Overflow See other posts from Stack Overflow or by Ishu
Published on 2010-12-29T13:47:35Z Indexed on 2010/12/29 13:54 UTC
Read the original article Hit count: 266

Filed under:
|

What i am doing, i downloded a code from calender now i want to show images on its tiles(for date).

What i am trying shows in code

- (void)drawTextInContext:(CGContextRef)ctx
{
    CGContextSaveGState(ctx);
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;

    CGFloat numberFontSize = floorf(0.3f * width);

CGContextSetFillColorWithColor(ctx, kDarkCharcoalColor);
CGContextSetTextDrawingMode(ctx, kCGTextClip);
for (NSInteger i = 0; i < [self.text length]; i++) {
    NSString *letter = [self.text substringWithRange:NSMakeRange(i, 1)];
    CGSize letterSize = [letter sizeWithFont:[UIFont boldSystemFontOfSize:numberFontSize]];

    CGContextSaveGState(ctx);  // I will need to undo this clip after the letter's gradient has been drawn
    [letter drawAtPoint:CGPointMake(4.0f+(letterSize.width*i), 0.0f) withFont:[UIFont boldSystemFontOfSize:numberFontSize]];

    if ([self.date isToday]) {
        CGContextSetFillColorWithColor(ctx, kWhiteColor);
        CGContextFillRect(ctx, self.bounds);  

    } else {
       // CGContextDrawLinearGradient(ctx, TextFillGradient, CGPointMake(0,0), CGPointMake(0, height/3), kCGGradientDrawsAfterEndLocation);
        CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename("left-arrow.png"); 
        CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);
        //UIImage* image = [UIImage imageNamed:@"left-arrow.png"];
        //CGImageRef imageRef = image.CGImage;

        CGContextDrawImage(ctx, CGRectMake(8.0f+(letterSize.width*i), 0.0f, 5, 5), image);
        //im.image=[UIImage imageNamed:@"left-arrow.png"];

    }

    CGContextRestoreGState(ctx);  // get rid of the clip for the current letter        
}

CGContextRestoreGState(ctx);

}

In else condition i want to show images on the tile so for that i am converting image objects in the CGContextDrawImage.

Please help me.

I am not sure this would be done in same manner or in other manner please suggest your way to do this.

Thanx a lot.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cgimage