iPhone:不显示组表格单元格边框

时间:2011-05-06 09:09:56

标签: iphone uitableview cgcontext

我正在使用附加代码来定制Group表的单元格背景。基本上我正在创建圆角图像并将其分配给表格单元格背景。在创建圆角图像时,我使用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;
}

0 个答案:

没有答案
相关问题