简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM