UIImageView和drawInRect有什么区别?
我想在表格单元格中显示这么多图像。我知道两种显示图像的方法。
一种是创建 UIImageView 实例并显示它
CGRect rect=CGRectMake(x,y,width,height);
UIImageView *image=[[UIImageView alloc]initWithFrame:rect];
[image setImage:[UIImage imageNamed:@"sample.jpg"]];
另一种方法是,
CGRect rect=CGRectMake(x,y,width,height);
[[UIImage imageNamed:@"sample.jpg"] drawInRect:rect];
现在,我的问题是,这两者之间有什么区别?哪一个是高效的?或者还有比这个更好的功能吗?
提前致谢....
I want to display so many images in table cells. I knew two methods to show an image.
One is creating an instance to UIImageView and show it
CGRect rect=CGRectMake(x,y,width,height);
UIImageView *image=[[UIImageView alloc]initWithFrame:rect];
[image setImage:[UIImage imageNamed:@"sample.jpg"]];
Another method is,
CGRect rect=CGRectMake(x,y,width,height);
[[UIImage imageNamed:@"sample.jpg"] drawInRect:rect];
Now, my question is, what is the difference between these two? Which one is efficient? Or someother function is available better than this?
Thanks in advance....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过使用
drawInRect:
方法,CoreGraphics 使用 CPU 将图像绘制到活动的CGContext
中。假设您使用UIView
的drawRect:
方法,这会将图像绘制到视图的缓冲区中。UIImageView
使用分配给它的图像作为后备缓冲区,而不是使用速度较慢的drawRect:
。然后,当通过 QuartzCore 合成屏幕时,GPU 会直接引用该缓冲区。Using the
drawInRect:
method has CoreGraphics draw the image into the activeCGContext
using the CPU. Assuming you're in aUIView
'sdrawRect:
method, this will paint the image into the view's buffer.UIImageView
uses whichever image is assigned to it as it's backing buffer instead of using the slowerdrawRect:
. The GPU then references this buffer directly when the screen is composited via QuartzCore.UIImageView
是UIView
子类。通过将其添加到视图层次结构中,您可以获得所有免费的好处:动画、大小属性、仿射变换等。此外,您还可以随时更改底层UIImage
。另一方面,调用
drawInRect:
只会在您指定的位置绘制图像,但不会与UIView
层次结构交互,因此您不会获得任何将其放在视图层次结构中会受益匪浅。我认为(没有尝试过),直接绘制图像更快,但我认为在大多数情况下使用
UIImageView
是一个更好的主意。UIImageView
is aUIView
subclass. By adding it to the view hierarchy you get all the free benefits: animations, size properties, affine transoforms, etc. Plus, you are able to change the underlyingUIImage
any time you want.On the other hand, calling
drawInRect:
will just draw the image where you tell it to, but not interact with theUIView
hierarchy, so you don't gain any of the benefits from having it on the view hierarchy.I would think (have not tried it), that drawing the image directly is faster, but I would think in most of the cases having a
UIImageView
is a better idea.