Drawing to the screen. – iFans – iPad, iPhone, and iPod touch Fans forums

This is a useful tutorial on how to draw to a UIImageView.

via [Tutorial] Drawing to the screen. – iFans – iPad, iPhone, and iPod touch Fans forums.

I made some changes to the code (not to say you need to): I…

  • subclass UIImageView
  • change drawImage.image to self.image
  • added to init: self.userInteractionEnabled = YES;
  • made the double-tap-to-clear a setting and added a similar setting for double-tap-to-erase
  • double-tap-to-erase uses CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear); – works like an eraser (otherwise use CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeColor);)
  • added UIColor and float setter for draw color and alpha (alpha sets self.alpha – works better than setting the stroke alpha)
  • use [strokeColor setStroke]; to set the stroke color
  • extracted out the context drawing into a method and call it w/ whatever the end point is (either currentPoint or lastPoint)

The result is a UIImageView subclass that takes a color and alpha and optionally either clears all or toggles erase mode w/ a double tap.