iPhone:如何向UIView的根层添加图层以显示包含content属性的图像? [英] iPhone: How do I add layers to UIView's root layer to display an image with the content property?

查看:82
本文介绍了iPhone:如何向UIView的根层添加图层以显示包含content属性的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Mac Dev Center文档,您应该能够设置CALayer的contents属性并自动渲染。但是,我仍然无法通过稍后向UIView的根添加子层来显示简单的图像。我尝试了多种不同的变化;这是我到目前为止所拥有的:

According to the Mac Dev Center docs, you should be able to set the contents property of a CALayer and have that render automatically. However, I still can't get a simple image to show up by adding a sublayer to the UIView's root later. I've tried multiple different variations; here's what I have so far:

(注意:我知道还有其他渲染图像的方法;出于我的目的,我想将CALayer用于其他一些方法复杂的东西,我准备进入)。

(Note: I know there are other ways of rendering images; for my purposes I'd like to use CALayer's for some of the more complicated stuff I'm going to get into).

(在ViewController的viewDidDisplay()中):

(in viewDidDisplay() of the ViewController):

 CALayer *theLayer = [CALayer layer];
 [[[self view] layer] addSublayer:theLayer];
 theLayer.contents = (id)[[UIImage imageNamed:@"mypic.png"] CGImage];
 theLayer.contentsRect = CGRectMake(0.0f, 0.0f, 300.0f, 300.0f);
 theLayer.bounds = CGRectMake(0.0f, 0.0f, 300.0f, 400.0f);

任何人都知道我做错了什么?

Anyone know what I'm doing wrong?

谢谢!

推荐答案

您无需设置 contentsRect (如果你这样做,它应该在单位坐标空间,可能只是 CGRectMake(0,0,1.0,1.0)。

You don't need to set the contentsRect (and if you do, it should be in the unit coordinate space, probably just CGRectMake(0, 0, 1.0, 1.0).

您可能需要设置图层位置属性。

You might want to set the layer's position property.

这篇关于iPhone:如何向UIView的根层添加图层以显示包含content属性的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆