objective-c – 如何将UIImage添加到分组的UITableViewCell,以便圆角?

我正在尝试将图像添加到分组的UITableView中的表格单元格中,但图像的边角不会被剪切.什么是最好的方式去剪裁这些(除了剪贴他们在Photoshop?表内容是动态的.)

例如,表中的第一个图像只需要左上角的四舍五入.

解决方法

这是我的解决方案,可以使用一些重构:
void addRoundedRectToPath(CGContextRef context,CGRect rect,float ovalWidth,float ovalHeight,BOOL top,BOOL bottom)
{
    float fw,fh;
    if (ovalWidth == 0 || ovalHeight == 0) {
        CGContextAddRect(context,rect);
        return;
    }
    CGContextSaveGState(context);
    CGContextTranslateCTM (context,CGRectGetMinX(rect),CGRectGetMinY(rect));
    CGContextScaleCTM (context,ovalWidth,ovalHeight);
    fw = CGRectGetWidth (rect) / ovalWidth;
    fh = CGRectGetHeight (rect) / ovalHeight;
    CGContextMovetoPoint(context,fw,fh/2);
    CGContextAddArcToPoint(context,fh,fw/2,0);

    NSLog(@"bottom? %d",bottom);

    if (top) {
        CGContextAddArcToPoint(context,fh/2,3);
    } else {
        CGContextAddArcToPoint(context,0);
    }

    if (bottom) {
        CGContextAddArcToPoint(context,0);
    }

    CGContextAddArcToPoint(context,0);
    CGContextClosePath(context);
    CGContextRestoreGState(context);
}

- (UIImage *)roundCornersOfImage:(UIImage *)source roundTop:(BOOL)top roundBottom:(BOOL)bottom {
    int w = source.size.width;
    int h = source.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL,w,h,8,4 * w,colorSpace,kCGImageAlphaPremultipliedFirst);

    CGContextBeginPath(context);
    CGRect rect = CGRectMake(0,h);
    addRoundedRectToPath(context,rect,4,top,bottom);
    CGContextClosePath(context);
    CGContextClip(context);

    CGContextDrawImage(context,CGRectMake(0,h),source.CGImage);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];    
}

实现这些功能,然后检查cellForRowAtIndexPath委托方法中的indexPath,以确定要舍入的角.

if (indexPath.row == 0) {
            cell.imageView.image = [self roundCornersOfImage:coverImage roundTop:YES roundBottom:NO];
        } else if (indexPath.row == [indexPath length]) {
            cell.imageView.image = [self roundCornersOfImage:coverImage roundTop:NO roundBottom:YES];
        } else {
            cell.imageView.image = coverImage;
        }

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...