2

Is there a way to black out a region (defined by a frame) of a UIImageView without creating an overlaying UIView instance (with this frame) and blacking that out?

Is there similarly a way to reveal part of a UIImageView without using UIView instances to black out the rest of the image?

Ken
  • 30,811
  • 34
  • 116
  • 155

2 Answers2

1

I am going to cheat a bit here and suggest that you use layer as they are light weight.

CALayer *blackLayer = [CALayer layer];
blackLayer.backgroundColor = [UIColor blackColor];
blackLayer.frame = imageView.bounds;
[imageView.layer addSublayer:blackLayer];

For the second part you can consider using a grid of layers (black). When user touches the image view, you can pick the layers from the area he has touched and remove them from the super layer i.e. the image view's root layer.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
0

you can subclass the UIIMageView and draw as per your needs in the drawRectMethod.

  1. You can create a subclass of UIIMageView and have your definition of drawRectMethod over there.
  2. You can also invoke the drawRectMethod at your will by calling setNeedsLayout method on the view .( setNeedsDisplay - causes drawRect to be called - . Only contentMode = UIViewContentModeRedraw will cause setNeedsDisplay to be called when a view's bounds changes).

UPDATE:

http://developer.apple.com/library/ios/#documentation/CoreGraphics/Reference/CoreGraphics_Framework/_index.html

RK-
  • 12,099
  • 23
  • 89
  • 155
  • Thanks for the update. I'm still unclear as to what goes in the `drawRect:` method. This is really what I'm after. – Ken May 20 '11 at 01:30
  • You can use CoreGraphics to draw what ever you want on the sub classed view in drawRect Method. In your case, I think you want to set a area to black color. This can be done using Core Graphics. http://developer.apple.com/library/ios/#documentation/CoreGraphics/Reference/CoreGraphics_Framework/_index.html – RK- May 20 '11 at 04:54