我使用UICollectionViewCell
尝试打印右上角的数字(从核心数据存储获得)以显示产品更新的数量。核心图形上下文似乎没有返回图像
随着细胞被再利用的方式,我发现如果使用代码绘制成UIView
层(使用的CALayer)中,当该单元被重复使用,CALayer
遗体并且通过错误的产品出现。显然,根据SO上的其他帖子,添加子视图到单元格不是一个聪明的事情,因为它们被重用等。
所以,我试图创建一个图像使用核心图形(在一个单独的方法)和将图像显示在容器UIImageView
中。在教程之后,我似乎无法获得图像返回。
我正在做电话为:
cell.messageCount.image = [self messageImageInRect:CGRectMake(48.0, 1.0, 21.0, 21.0)];
的方法是:
-(UIImage *)messageImageInRect:(CGRect)rect {
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
NSLog(@"I am inside");
NSLog(@"I have rect as: %@", NSStringFromCGRect(rect));
NSLog(@"I have the size as: %@", NSStringFromCGSize(rect.size));
NSLog(@"I have the context as: %@", context);
CGContextSetLineWidth(context, 2.0f);
CGContextAddEllipseInRect(context, rect);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(context);
CGContextStrokePath(context);
UIImage *testImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImage;
}
这似乎是很基本的。
NSLog语句有助于试图弄清楚发生了什么。但是,根据日志,它似乎工作,但是,我没有图像被返回(或至少显示)。我不知道为什么。
日志显示:
2014-01-19 18:06:02.753 XXXXXXXX[62921:70b] I am inside
2014-01-19 18:06:02.754 XXXXXXXX[62921:70b] I have rect as: {{48, 1}, {21, 21}}
2014-01-19 18:06:02.754 XXXXXXXX[62921:70b] I have the size as: {21, 21}
2014-01-19 18:06:02.754 XXXXXXXX[62921:70b] I have the context as: <CGContext 0x10f133640>
2014-01-19 18:06:02.754 XXXXXXXX[62921:70b] I have test Image size as: {21, 21}
随着messageCount被在UICollectionViewCell定义。
正如往常一样,这一直是约3小时我的时间,看完教程后教程和SO响应后SO响应和编程手册...
任何帮助,将不胜感激。
您是救命的人!谢谢。 “CGContextDrawPath”令人遗憾的一个新手错误 - 我从早期的尝试中删除了它。用'rect.origin'的东西,我假设这会把我提供的'rect'的坐标移动到我想要绘制的'rect'上? – Darren
对,我想你明白了。 –