iPhone:组表单元格边框不出现
我正在使用附加代码来自定义组表的单元格背景。基本上我正在创建圆角图像并将其分配给表格单元格背景。在创建圆角图像时,我使用 CGContextSetLineWidth 和 CGContextSetLineWidth 绘制图像周围的边框,但它似乎不起作用。谁能告诉我如何让它工作?我做错了什么?还有其他方法可以让边框包围我的圆形图像吗?
谢谢。
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:"cellbg.png"];
if (tableView.style == UITableViewStyleGrouped) {
CustomBackgroundView *bgView = [[[CustomBackgroundView alloc] init] autorelease];
bgView.bgimg = cellImageView.image;
bgView.borderColor = [UIColor lightGrayColor];
bgView.fillColor = [UIColor redColor];
// Create rounded corner image
cellImageView.image = [bgView roundedCornerImage:20];
cell.backgroundView = [[[UIImageView alloc] initWithImage:cellImageView.image] autorelease];
[cellImageView release];
}
/* CustomBackgroundView implementation */
-(UIImage *)roundedCornerImage:(NSInteger)cornerSize {
// If the image does not have an alpha layer, add one
UIImage *image = [bgimg imageWithAlpha];
// Build a context that's the same dimensions as the new size
CGContextRef context = CGBitmapContextCreate(NULL,
image.size.width - 25,
image.size.height,
CGImageGetBitsPerComponent(image.CGImage),
0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
CGContextSetFillColorWithColor(context, [fillColor CGColor]);
CGContextSetStrokeColorWithColor(context, [borderColor CGColor]);
CGContextSetLineWidth(context, 2);
// Create a clipping path with rounded corners
CGContextBeginPath(context);
CGRect rect = CGRectMake(0, 0, image.size.width - 25, image.size.height);
CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
minx = minx + 1;
miny = miny ;
maxx = maxx - 1;
maxy = maxy - 1;
CGContextMoveToPoint(context, minx, miny);
CGContextAddArcToPoint(context, minx, maxy, midx, maxy, cornerSize);
CGContextAddArcToPoint(context, maxx, maxy, maxx, miny, cornerSize);
CGContextAddLineToPoint(context, maxx, miny);
CGContextClosePath(context);
CGContextClip(context);
// Draw the image to the context; the clipping path will make anything outside the rounded rect transparent
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width - 25, image.size.height), image.CGImage);
CGContextDrawPath(context, kCGPathFillStroke);
// Create a CGImage from the context
CGImageRef clippedImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// Create a UIImage from the CGImage
UIImage *roundedImage = [UIImage imageWithCGImage:clippedImage];
CGImageRelease(clippedImage);
return roundedImage;
}
I am using attached code for customized cell background of Group table. Basically I am creating the rounded corner image and assigning it to table cell background. While creating rounded corner image, I am using CGContextSetLineWidth and CGContextSetLineWidth to draw a border surrounding the image but it doesn't seem to work. Could anyone please tell me how could I get it work? What am I doing wrong? Is there any other way to get the border surrounded my rounded image?
Thanks.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:"cellbg.png"];
if (tableView.style == UITableViewStyleGrouped) {
CustomBackgroundView *bgView = [[[CustomBackgroundView alloc] init] autorelease];
bgView.bgimg = cellImageView.image;
bgView.borderColor = [UIColor lightGrayColor];
bgView.fillColor = [UIColor redColor];
// Create rounded corner image
cellImageView.image = [bgView roundedCornerImage:20];
cell.backgroundView = [[[UIImageView alloc] initWithImage:cellImageView.image] autorelease];
[cellImageView release];
}
/* CustomBackgroundView implementation */
-(UIImage *)roundedCornerImage:(NSInteger)cornerSize {
// If the image does not have an alpha layer, add one
UIImage *image = [bgimg imageWithAlpha];
// Build a context that's the same dimensions as the new size
CGContextRef context = CGBitmapContextCreate(NULL,
image.size.width - 25,
image.size.height,
CGImageGetBitsPerComponent(image.CGImage),
0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
CGContextSetFillColorWithColor(context, [fillColor CGColor]);
CGContextSetStrokeColorWithColor(context, [borderColor CGColor]);
CGContextSetLineWidth(context, 2);
// Create a clipping path with rounded corners
CGContextBeginPath(context);
CGRect rect = CGRectMake(0, 0, image.size.width - 25, image.size.height);
CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
CGFloat miny = CGRectGetMinY(rect) , maxy = CGRectGetMaxY(rect) ;
minx = minx + 1;
miny = miny ;
maxx = maxx - 1;
maxy = maxy - 1;
CGContextMoveToPoint(context, minx, miny);
CGContextAddArcToPoint(context, minx, maxy, midx, maxy, cornerSize);
CGContextAddArcToPoint(context, maxx, maxy, maxx, miny, cornerSize);
CGContextAddLineToPoint(context, maxx, miny);
CGContextClosePath(context);
CGContextClip(context);
// Draw the image to the context; the clipping path will make anything outside the rounded rect transparent
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width - 25, image.size.height), image.CGImage);
CGContextDrawPath(context, kCGPathFillStroke);
// Create a CGImage from the context
CGImageRef clippedImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// Create a UIImage from the CGImage
UIImage *roundedImage = [UIImage imageWithCGImage:clippedImage];
CGImageRelease(clippedImage);
return roundedImage;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论