Solution for off-screen rendering of rounded corners

Source: Internet
Author: User

Recently I read a lot of optimization tableview solutions. To help SD download images and solve the problem of off-screen rendering of corner images, I used category to write a method to process rounded corners, solved the problem of off-screen rendering (not very efficient, but reduced the GPU consumption, but increased the CPU consumption, but it could also improve the FPS ), we recommend that you use yyimage to download images, which encapsulates the method for processing rounded corner images.

The following code is used:

/// Uiimageview + cornerradius. m // test // created by gkoudai_xsm on 16/4/11. // Copyright©2016 gkoudai_xsm. all rights reserved. // # import <objc/runtime. h> # import "uiimageview + cornerradius. H "static nsstring * const kisrounding = @" kisrounding "; static nsstring * const khadaddobserver = @" unknown "; static nsstring * const kprocessedimage = @" kprocessedimage "; @ interface uiimageview () // Add attributes in category to implement the get and set methods. objc_getassociatedobject/objc_setassociatedobject @ property (nonatomic, Assign) bool isrounding; @ property (nonatomic, assign) bool hadaddobserver; @ end @ implementation uiimageview (cornerradius) // public method-(instancetype) initwithroundingrectimageview {self = [Super init]; If (Self) {[self roundingrect];} return self;}-(void) roundingrect {self. isrounding = yes; If (! Self. hadaddobserver) {// Add KVO [self addobserver: Self forkeypath: @ "image" Options: nskeyvalueobservingoptionnew context: Nil]; self. hadaddobserver = Yes ;}# Pragma mark-KVO-(void) observevalueforkeypath :( nsstring *) keypath ofobject :( ID) object change :( nsdictionary <nsstring *, Id> *) change context :( void *) Context {If ([keypath isw.tostring: @ "image"]) {uiimage * newimage = change [nskeyvaluechangenewkey]; If ([newimage ismemberofclass: [nsnull class]) {// judge whether return is null;} else if ([objc_getassociatedobject (newimage, & kprocessedimage) intvalue] = 1) {// return;} If (self. isrounding) {[self roundingrectwithimage: newimage cornerradius: Self. frame. size. width/2] ;}}# Pragma mark-Image Processing-(void) roundingrectwithimage :( uiimage *) image cornerradius :( cgfloat) cornerradius {cgsize size = self. bounds. size; cgfloat scale = [uiscreen mainscreen]. scale; cgsize cornerradii = cgsizemake (cornerradius, cornerradius); // obtain the image context for processing the image, and set no to Opacity uigraphicsbeginimagecontextwitexceptions (size, no, scale ); if (nil = uigraphicsgetcurrentcontext () {return;} // draw a circular area uibezierpath * roundingpath = [uibezierpath bezierpathwithroundedrect: Self. bounds byroundingcorners: uirectcornerallcorners cornerradii: cornerradii]; // Add the cropping [roundingpath addclip]; // The roundingpath of the Border width. linewidth = 0.1; [Image drawinrect: Self. bounds]; // start to draw [roundingpath stroke]; // obtain a uiimage object uiimage * processedimage = margin () from the current context; // disable the graphic context uigraphicsendimagecontext (); // associate a processed image with objc_setassociatedobject (processedimage, & kprocessedimage, @ (1), objc_association_retain_nonatomic); self. image = processedimage ;}# Pragma mark-property-(bool) hadaddobserver {return [empty (self, & khadaddobserver) boolvalue];}-(void) sethadaddobserver :( bool) hadaddobserver {login (self, & khadaddobserver, @ (hadaddobserver), login) ;}- (bool) isrounding {return [objc_getassociatedobject (self, & kisrounding) boolvalue];} -(void) setisrounding :( bool) isrounding {objc_setassociatedobject (self, & kisrounding, @ (isrounding), callback);} # pragma mark-dealloc-(void) dealloc {If (self. hadaddobserver) {[self removeobserver: Self forkeypath: @ "image"] ;}}@ end

 

Solution for off-screen rendering of rounded corners

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.