UIImageView和drawInRect有什么区别?

发布于 2024-09-26 01:05:54 字数 457 浏览 1 评论 0原文

我想在表格单元格中显示这么多图像。我知道两种显示图像的方法。

一种是创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山有枢 2024-10-03 01:05:54

通过使用 drawInRect: 方法,CoreGraphics 使用 CPU 将图像绘制到活动的 CGContext 中。假设您使用 UIViewdrawRect: 方法,这会将图像绘制到视图的缓冲区中。

UIImageView 使用分配给它的图像作为后备缓冲区,而不是使用速度较慢的 drawRect:。然后,当通过 QuartzCore 合成屏幕时,GPU 会直接引用该缓冲区。

Using the drawInRect: method has CoreGraphics draw the image into the active CGContext using the CPU. Assuming you're in a UIView's drawRect: 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 slower drawRect:. The GPU then references this buffer directly when the screen is composited via QuartzCore.

桃扇骨 2024-10-03 01:05:54

UIImageViewUIView 子类。通过将其添加到视图层次结构中,您可以获得所有免费的好处:动画、大小属性、仿射变换等。此外,您还可以随时更改底层 UIImage

另一方面,调用 drawInRect: 只会在您指定的位置绘制图像,但不会与 UIView 层次结构交互,因此您不会获得任何将其放在视图层次结构中会受益匪浅。

我认为(没有尝试过),直接绘制图像更快,但我认为在大多数情况下使用 UIImageView 是一个更好的主意。

UIImageView is a UIView 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 underlying UIImage any time you want.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文