在 CGContextRef 中显示图像

发布于 2024-10-09 17:46:02 字数 1892 浏览 5 评论 0原文

我正在做什么,我下载了日历的代码,现在我想在其图块上显示图像(用于日期)。

我正在尝试在代码中显示

- (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);

}

在其他情况下,我想在图块上显示图像,因此我正在转换 CGImageRef 中的图像对象。

请帮我。

我不确定这是否会以相同的方式或其他方式完成,请建议您的方式来做到这一点。

非常感谢。

What i am doing, i downloded a code for 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 CGImageRef.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-10-16 17:46:02

图像的文件路径似乎有问题。您可以使用 NSBundle 方法检索正确的路径。此外,您还泄漏了大量内存,因为您没有释放图像和数据提供程序。长话短说,试试这个:

[[UIImage imageNamed:@"left-arrow.png"] drawInRect:...]

或者更简单:

[[UIImage imageNamed:@"left-arrow.png"] drawAtPoint:...]

The file-path of the image seems to problematic. You can retrieve the correct path with the NSBundle-methods. Also you're leaking a lot of memory, because you don't release your images and data-providers. To make a long story short, try this:

[[UIImage imageNamed:@"left-arrow.png"] drawInRect:...]

or even simpler:

[[UIImage imageNamed:@"left-arrow.png"] drawAtPoint:...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文