在 UITableView 中绘制 CGImage

发布于 2024-12-02 16:53:38 字数 762 浏览 1 评论 0原文

所以,我有一个自定义单元格,我需要在 tableView 中将所有图像绘制为 CGImage,但我无法让它工作。我创建了一个测试项目并使用简单的视图测试了代码。一切都很完美,当我将相同的代码复制粘贴到我的自定义单元格时,它停止工作。这是代码:

-(void)drawRect:(CGRect)rect {
    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;

    UIImage *karmaImage = [UIImage imageNamed:@"karma.png"];
    [self drawImage:karmaImage withRect:CGRectMake(boundsX + 255, 16, 14, 14)];
}
-(void)drawImage:(UIImage *)image withRect:(CGRect)rect {
    CGImageRef imageRef = CGImageRetain(image.CGImage);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, rect, imageRef);
}

有解决方案吗?

So, I have a custom cell and I need to draw all images as CGImage in tableView, but I can't get it working. I have created a test project and tested the code with simple views. Everything worked perfect, and when I copypasted the same code to my custom cell it stopped working. Here is the code:

-(void)drawRect:(CGRect)rect {
    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;

    UIImage *karmaImage = [UIImage imageNamed:@"karma.png"];
    [self drawImage:karmaImage withRect:CGRectMake(boundsX + 255, 16, 14, 14)];
}
-(void)drawImage:(UIImage *)image withRect:(CGRect)rect {
    CGImageRef imageRef = CGImageRetain(image.CGImage);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, rect, imageRef);
}

Any solutions?

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

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

发布评论

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

评论(3

奢华的一滴泪 2024-12-09 16:53:38

Apple 建议将自定义视图添加到 UITableViewCell 的 contentView,而不是更改 UITableViewCell 本身。请参阅 TimeZoneCell 对于 例子。

Apple recommends to add your custom view to the UITableViewCell's contentView instead of changing UITableViewCell itself. See TimeZoneCell for an example.

故人如初 2024-12-09 16:53:38

在你的drawRect方法中,你可能应该调用[super drawRect:rect];

In your drawRect method, you should probably call [super drawRect:rect];

把人绕傻吧 2024-12-09 16:53:38

好的,问题是具有自定义背景颜色的单元格的 contentView 隐藏了图像。这是正确的代码:

-(void) drawRect:(CGRect)rect{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:1.0].CGColor);
    CGContextFillRect(context, rect);
    UIImage *karmaImg = [UIImage imageNamed:@"karma.png"]; 
    [self drawImage:karmaImg withContext:context atPoint:CGPointMake(boundsX + 255, 16)];
}


-(void)drawImage:(UIImage *)image withContext:(CGContextRef)context atPoint:(CGPoint)point {
    if(image) {
        CGContextDrawImage(context, CGRectMake(point.x, point.y, image.size.width, image.size.height), image.CGImage);
    } else {
        NSLog(@"Error: Image failed to load.");
    }
}

OK, the problem was that contentView of the cell with custom background color was hiding the image. Here is the right code:

-(void) drawRect:(CGRect)rect{
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:1.0].CGColor);
    CGContextFillRect(context, rect);
    UIImage *karmaImg = [UIImage imageNamed:@"karma.png"]; 
    [self drawImage:karmaImg withContext:context atPoint:CGPointMake(boundsX + 255, 16)];
}


-(void)drawImage:(UIImage *)image withContext:(CGContextRef)context atPoint:(CGPoint)point {
    if(image) {
        CGContextDrawImage(context, CGRectMake(point.x, point.y, image.size.width, image.size.height), image.CGImage);
    } else {
        NSLog(@"Error: Image failed to load.");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文