General Pattern For Rendering Cached UIImages in drawRect:

I’m working on a project right now where we have a lot of custom UIViews.  It would be a nightmare to ask our designer to make .PNGs for every type of situation.  So instead he provides me .SVG files and I import them into PaintCode (PaintCode 1, PaintCode 2 has been released but they are not providing free upgrades and the tool is way too expensive IMHO, seeing as I paid €90 not even 6 months ago and have used it sporadically).  PaintCode itself is a useful tool, but still requires some further ‘data massaging’ to get things to really render as I’d like them.

Anyway, what I tend to do is render these views once into UIImages and put them in a cache, retrieving them again via a cache key that uses the UIView subclass’s current state to generate the key.

I’d like to post the general approach I take so that all you have to do is retrieve the image from the cache (or generate it) then in your drawRect method you call [image drawInRect: rect]; and you’re done.

NOTE, this won’t work immediately for you because of a UIColor to hexString category, but at least you see here the general approach that is taken.  And I can copy-paste for my own purposes in the future.  😉

  #import "UIImage+LPGlowingTextFieldBackground.h"
  #import "UIColor+HexColors.h"
  static NSCache *kImageCacheGlowingTextField = nil;
  
  @implementation UIImage (LPGlowingTextFieldBackground)
  
  + (NSString*)cacheKeyForSize:(CGSize)imgSize 
                   borderColor:(UIColor*)borderColor 
               backgroundColor:(UIColor*)bgColor 
                     glowColor:(UIColor*)glowColor 
                     intensity:(int)intensity
 {
    NSString *glowPortion = nil;
    if (glowColor && intensity != 0) {
      glowPortion = [NSString stringWithFormat:@"_G:%@_I:%i", [UIColor hexValuesFromUIColor:glowColor], intensity];
    }
  
    NSString *result = [NSString stringWithFormat:@"%ix%i_F:%@_B:%@", (int)imgSize.width, (int)imgSize.height, [UIColor hexValuesFromUIColor:borderColor], [UIColor hexValuesFromUIColor:bgColor]];
    
    if (glowPortion) {
      result = [result stringByAppendingString:glowPortion];
    }
    
    return result;
  }
  
  + (UIImage*)backgroundImageForTextFieldWithSize:(CGSize)imgSize
                                      borderColor:(UIColor*)borderColor
                                  backgroundColor:(UIColor*)bgColor
                                        glowColor:(UIColor*)glowColor
                                        intensity:(int)intensity
  {
  
    if (!kImageCacheGlowingTextField) {
      kImageCacheGlowingTextField = [[NSCache alloc] init];
    }
  
    // clamp intensity
    intensity = MAX(0, intensity);
  
    NSString *cacheKey = [self cacheKeyForSize:imgSize 
                                   borderColor:borderColor 
                               backgroundColor:bgColor 
                                     glowColor:glowColor 
                                     intensity:intensity];
    UIImage *result = nil;
  
    result = [kImageCacheGlowingTextField objectForKey:cacheKey];
    if (result) {
      return result;
    }
  
    UIGraphicsBeginImageContextWithOptions(imgSize, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
  
    // CUSTOM CODE HERE!!
  
    // END CUSTOM DRAWING CODE
  
    result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [kImageCacheGlowingTextField setObject:result forKey:cacheKey];
   
    return result;
  }
  
  @end

Man I need to get a CodeFormatter for this site. Sorry!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s