1

A little background: I'm working on an iOS app that has a variety of status icons for various states. These icons are used in a variety of places and sizes including as UITableViewCell imageViews, as custom MKMapAnnotations and a few other spots. I actually have a couple sets which include a more static status icon as well as ones that have dynamic text injected into the design.

So at first I went the conventional route of using static raster assets, but because the sizes were dynamic this wasn't always the best solution and I wasn't thrilled with the quality of the scaling using CGAffineTransforms. So instead I changed gears a bit and tried something else:

  • Created a custom UIView subclass for each high level class of icon. It takes as input the model object that derives the status from (I suppose I could have also just used an enum and loaded this into some kind of model constructor but this is how I did it) so it can decide what it needs to draw, then does the necessary drawing in drawRect. Since all of the drawing is based on the view bounds it scales to any reasonable dimensions.
  • Created a Category which has class method constructors that take the model inputs as well as the size you want to use and constructs the custom views.
  • Since I also wanted the option to have rasterized versions of these icons to plug into certain places (such as a UITableViewCell imageView) I also created constructors that build the view and return a UIImage using the fast iOS7 snapshotting functions.

So what does this give me? Well here's the pros/cons that I can see.

Pros

  • Completely scalable graphics that can easily be used in a variety of different scenarios and contexts.
  • Easy compatibility with adding dynamic info to the graphics such as text. Because I have the exact shape data on everything I'm drawing I don't need to guesstimate on the bounds for a text box since I know how everything is laid out.
  • Compatibility with situations where I might want a rasterized asset but I still get all the advantages of the dynamic view since I'm not rasterizing it till I need it.
  • Reduces the size of the application since I don't need to include raster assets.

Cons

  • The workflow for creating the draw code in the first place isn't ideal. For simple stuff I can do it straight in code but for more complex things I'll need to create the vector asset in Illustrator or Sketch then bring it into PaintCode and clean up the generated draw code into something more streamlined. This is not the most ideal process.

So the question is: does anyone have any better suggestions for how to deal with this sort of situation? I haven't found an enormous amount of material on techniques for this sort of thing and I'm wondering if I'm missing a better way of handling this or if there are any hidden gotchas here...performance doesn't seem to be an issue from my testing with my approach but I haven't tested it on the iPad3 or iPhone 4 yet so there could still be some unknowns.

Jay
  • 6,572
  • 3
  • 37
  • 65
smyrgl
  • 864
  • 6
  • 12

1 Answers1

0

You could try SVGKit, which draws SVG files, and can export to a UIImage, if desired.

Austin
  • 5,625
  • 1
  • 29
  • 43
  • Yes, but then I give up access to CoreText/TextKit as well as any dynamic layout that I might want to do. The only real advantage it seems to have is making the production pipeline easier. I'm also assuming the generated code is not going to be as streamlined as the stuff I can draw myself but that's probably a minor point. – smyrgl Mar 25 '14 at 21:17
  • There isn't a native way to render vector graphics files, so AFAIK you would either keep doing what you're doing, or you'd use a third-party library. I've used an approach like yours before (tweaking PaintCode output), but cached some commonly-reused graphics to boost the performance. – Austin Mar 25 '14 at 21:27
  • Yeah I didn't mention caching but I was thinking about a global image cache for the non-dynamic stuff. Not sure if it is necessary though...I'll need to try some table view scrolling tests to be sure. Since the snapshot API is so performant in iOS7 I suspect this kind of optimization may not be worth the memory usage but I'll get some hard data to be sure. – smyrgl Mar 25 '14 at 22:06
  • How do I tint the image? – fatuhoku May 15 '14 at 15:37