简体   繁体   English

UIImageView和drawInRect有什么区别?

[英]what is the difference between UIImageView and drawInRect?

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 一个是为UIImageView创建一个实例并显示它

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.... 提前致谢....

Using the drawInRect: method has CoreGraphics draw the image into the active CGContext using the CPU. 使用drawInRect:方法,CoreGraphics使用CPU将图像绘制到活动的CGContext Assuming you're in a UIView 's drawRect: method, this will paint the image into the view's buffer. 假设你在UIViewdrawRect:方法中,这会将图像绘制到视图的缓冲区中。

UIImageView uses whichever image is assigned to it as it's backing buffer instead of using the slower drawRect: . UIImageView使用分配给它的任何图像作为它的后备缓冲区,而不是使用较慢的drawRect: . The GPU then references this buffer directly when the screen is composited via QuartzCore. 然后,当通过QuartzCore合成屏幕时,GPU直接引用此缓冲区。

UIImageView is a UIView subclass. UIImageView是一个UIView子类。 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 underlying UIImage any time you want. 通过将其添加到视图层次结构中,您可以获得所有免费优势:动画,大小属性,仿射变换等。此外,您可以随时更改底层UIImage

On the other hand, calling drawInRect: will just draw the image where you tell it to, but not interact with the UIView hierarchy, so you don't gain any of the benefits from having it on the view hierarchy. 另一方面,调用drawInRect:将只绘制您告诉它的图像,但不与UIView层次结构交互,因此您不会从视图层次结构中获得任何好处。

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. 我认为(没有尝试过),直接绘制图像的速度更快,但我认为在大多数情况下使用UIImageView是一个更好的主意。

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

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