iPhone: граница ячейки таблицы группы не отображается - PullRequest
1 голос
/ 06 мая 2011

Я использую прикрепленный код для настраиваемого фона ячейки таблицы группы.В основном я создаю изображение с закругленными углами и назначаю его фону ячейки таблицы.При создании изображения с закругленными углами я использую 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;
}
...