iPhone:组表单元格边框不出现

发布于 2024-11-05 18:14:06 字数 3192 浏览 0 评论 0原文

我正在使用附加代码来自定义组表的单元格背景。基本上我正在创建圆角图像并将其分配给表格单元格背景。在创建圆角图像时,我使用 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文