简体   繁体   English

iPhone - 关于drawRect的问题

[英]iPhone - a question about drawRect

Suppose that instead a UIView I subclass a UIImageView and assign a background image to it, like in 假设相反,UIView我将UIImageView子类化并为其分配背景图像,就像在

UIImageView *myImageView = [[UIImageView alloc] initWithImage: //... bla bla

and then I implement a drawRect method and use it to draw stuff on the image. 然后我实现了一个drawRect方法并用它来绘制图像上的东西。

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. 如果覆盖drawRect,则会在图像本身上绘制内容。 Every UIImage is backed by a CALayer , so you're really drawing everything onto that. 每个UIImage都有一个CALayer支持,所以你真的把所有东西都画上了。 Access it with 访问它

[image layer];

You should subclass UIView, not UIImageView if you need custom drawing. 如果需要自定义绘图,则应该是UIView的子类,而不是UIImageView。

From the UIImageView class reference 来自UIImageView类的引用

The UIImageView class is optimized to draw its images to the display. UIImageView类经过优化,可将其图像绘制到显示器上。 UIImageView will not call drawRect: a subclass. UIImageView不会调用drawRect:一个子类。 If your subclass needs custom drawing code, it is recommended you use UIView as the base class. 如果您的子类需要自定义绘图代码,建议您使用UIView作为基类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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