objective-c - 如何将 UIImage 添加到分组的 UITableViewCell,使其圆角?

标签 objective-c iphone-sdk-3.0 uitableview rounded-corners

我正在尝试将图像添加到分组的 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, fw, fh, fw/2, fh, 0);

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

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

    if (bottom) {
        CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 3);
    } else {
        CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 0);
    }

    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 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, 0, w, h);
    addRoundedRectToPath(context, rect, 4, 4, top, bottom);
    CGContextClosePath(context);
    CGContextClip(context);

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

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

    return [UIImage imageWithCGImage:imageMasked];    
}

实现这些函数,然后检查 cellForRowAtIndexPath 委托(delegate)方法中的 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;
        }

关于objective-c - 如何将 UIImage 添加到分组的 UITableViewCell,使其圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2118613/

相关文章:

ios - 一次在 Firebase 中填充 UITableView 一组单元格

ios - 如何在不担心索引的情况下添加到 NSDictionary?实际上是一个 NSArray,每个索引有多个元素。有更好的解决方案吗?

iphone - iPhone 的自定义标签栏将标签纵向更改为横向

iphone-sdk-3.0 - UIWebView 向下滚动

iphone - iPad侧边栏界面

ios - dequeueReusableCell 中的 SIGABRT 错误(使用自定义 UITableViewCell 时带有标识符 :),

ios - 从 .plist 文件中读取

ios - 按下发送按钮后,MFMessageComposeViewController 消息消失且无法发送(iOS 7)

objective-c - 如何将自定义图钉添加到 iPhone MapKit?

iPhone水平滚动表