在UITableViewCell中的UIImageView上使用cornerRadius [英] Using cornerRadius on a UIImageView in a UITableViewCell

查看:166
本文介绍了在UITableViewCell中的UIImageView上使用cornerRadius的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为每个UITableViewCells使用一个UIImageView作为缩略图.我的代码使用SDWebImage从后端异步获取这些图像并将其加载,然后进行缓存.一切正常.

I'm using a UIImageView for each of my UITableViewCells, as thumbnails. My code uses SDWebImage to asynchronously grab those images from my backend and load them in, and then caching them. This is working fine.

我的UIImageView是一个50x50的正方形,在Interface Builder中创建.它的背景是不透明的白色(与我的UITableViewCell背景色相同,以提高性能).但是,我想使用光滑的角以获得更好的美观.如果我这样做:

My UIImageView is a 50x50 square, created in Interface Builder. Its background is opaque, white color (same as my UITableViewCell background color, for performance). However, I'd like to use smooth corners for better aesthetics. If I do this:

UIImageView *itemImageView = (UIImageView *)[cell viewWithTag:100];
    itemImageView.layer.cornerRadius = 2.5f;
    itemImageView.layer.masksToBounds = NO;
    itemImageView.clipsToBounds = YES;

我的表格视图立即下降约5-6帧,在快速滚动过程中上限为56 FPS(可以),但是当我拖动刷新控件时,它会滞后一点并下降到40 FPS.如果删除cornerRadius行,则一切正常,没有滞后.已使用乐器在iPod touch 5G上进行了测试.

My tableview drops around 5-6 frames immediately, capping about 56 FPS during fast scrolling (which is okay), but when I drag and pull the refresh control, it lags a bit and drops to around 40 FPS. If I remove the cornerRadius line, all is fine and no lag. This has been tested on an iPod touch 5G using Instruments.

还有其他方法可以使单元格的UIImageView取整而不遭受性能影响吗?我已经优化了cellForRowAtIndexPath并在没有cornerRadius的情况下快速滚动时获得了56-59 FPS.

Is there any other way I could have a rounded UIImageView for my cells and not suffer a performance hit? I'd already optimized my cellForRowAtIndexPath and I get 56-59 FPS while fast scrolling with no cornerRadius.

推荐答案

是的,这是因为CornerRadius和clipToBounds需要屏幕外渲染,我建议您从我的

Yes, that's because cornerRadius and clipToBounds requires offscreen rendering, I suggest you to read these answer from one of my question. I also quote two WWDC session thatyou should see.
The best thing you can do is grab the image right after is downloaded and on another thread dispatch a method that round the images. Is preferable that you work on the image instead of the imageview.

// Get your image somehow
UIImage *image = [UIImage imageNamed:@"image.jpg"];

// Begin a new image that will be the new image with the rounded corners 
// (here with the size of an UIImageView)
 UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);

 // Add a clip before drawing anything, in the shape of an rounded rect
  [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                        cornerRadius:10.0] addClip];
 // Draw your image
[image drawInRect:imageView.bounds];

 // Get the image, here setting the UIImageView image
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();

 // Lets forget about that we were drawing
  UIGraphicsEndImageContext();

此处抓取了方法 您还可以继承tableviewcell的子类并覆盖drawRect方法.
肮脏但非常有效的方法是在photoshop中使用内部alpha并在单元格背景的匹配颜色周围绘制蒙版,并在具有图像的蒙版上添加另一个imageView,而不是不透明且具有清晰背景色.

Method grabbed here You can also subclass the tableviewcell and override the drawRect method.
The dirty but very effective way is draw a mask in photoshop with inside alpha and around the matching color of the background of the cell and add another imageView, not opaque with clear background color, on the one with images.

这篇关于在UITableViewCell中的UIImageView上使用cornerRadius的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆