开发者

iPhone - a question about drawRect

Suppose that instead a UIView I subclass a UIImageView and assign a background image to it, like in

UIImageView *myImageView = [[UIImageView alloc] initWithImage: //... bla bla
开发者_运维技巧

and then I implement a drawRect method and use it to draw stuff on the image.

My question is:

  1. will the stuff being drawn on the image itself or in a kind of "layer" totally independent?
  2. if the later is true, how do I access this "layer"?

thanks


If you override drawRect, the stuff will be drawn on the image itself. Every UIImage is backed by a CALayer, so you're really drawing everything onto that. Access it with

[image layer];


You should subclass UIView, not UIImageView if you need custom drawing.

From the UIImageView class reference

The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜