objective c - Using cornerRadius on a UIImageView in a UITableViewCell -


i'm using uiimageview each of uitableviewcells, thumbnails. code uses sdwebimage asynchronously grab images backend , load them in, , caching them. working fine.

my uiimageview 50x50 square, created in interface builder. background opaque, white color (same uitableviewcell background color, performance). however, i'd use smooth corners better aesthetics. if this:

uiimageview *itemimageview = (uiimageview *)[cell viewwithtag:100];     itemimageview.layer.cornerradius = 2.5f;     itemimageview.layer.maskstobounds = no;     itemimageview.clipstobounds = yes; 

my tableview drops around 5-6 frames immediately, capping 56 fps during fast scrolling (which okay), when drag , pull refresh control, lags bit , drops around 40 fps. if remove cornerradius line, fine , no lag. has been tested on ipod touch 5g using instruments.

is there other way have rounded uiimageview cells , not suffer performance hit? i'd optimized cellforrowatindexpath , 56-59 fps while fast scrolling no cornerradius.

yes, that's because cornerradius , cliptobounds requires offscreen rendering, suggest read these answer 1 of question. quote 2 wwdc session thatyou should see.
the best thing can grab image right after downloaded , on thread dispatch method round images. preferable work on image instead of imageview.

// image somehow uiimage *image = [uiimage imagenamed:@"image.jpg"];  // begin new image new image rounded corners  // (here size of uiimageview)  uigraphicsbeginimagecontextwithoptions(imageview.bounds.size, no, 1.0);   // add clip before drawing anything, in shape of rounded rect   [[uibezierpath bezierpathwithroundedrect:imageview.bounds                          cornerradius:10.0] addclip];  // draw image [image drawinrect:imageview.bounds];   // image, here setting uiimageview image   imageview.image = uigraphicsgetimagefromcurrentimagecontext();   // lets forget drawing   uigraphicsendimagecontext(); 

method grabbed here can subclass tableviewcell , override drawrect method.
dirty effective way draw mask in photoshop inside alpha , around matching color of background of cell , add imageview, not opaque clear background color, on 1 images.


Comments