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

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

概述我正在尝试将图像添加分组的UITableView中的表格单元格中,但图像的边角不会被剪切.什么是最好的方式去剪裁这些(除了剪贴他们在Photoshop?表内容是动态的.) 例如,表中的第一个图像只需要左上角的四舍五入. 这是我的解决方案,可以使用一些重构: void addRoundedRectToPath(CGContextRef context, CGRect rect, float ova 我正在尝试将图像添加到分组的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;        }
总结

以上是内存溢出为你收集整理的objective-c – 如何将UIImage添加到分组的UITableViewCell,以便圆角?全部内容,希望文章能够帮你解决objective-c – 如何将UIImage添加到分组的UITableViewCell,以便圆角?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1232835.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存