How do I set the contents of a CALayer to a CGImageRef?

大兔子大兔子 提交于 2020-01-03 08:29:29

问题


I want to set the contents of a CALayer to an image. The CALayer has a contents property The documentation on that property says that "a layer can set this property to a CGImageRef to display the image as its contents." But the property takes an id so in Xcode I get the following issue:

Semantic Issue: Incompatible pointer types assigning to 'id' from 'CGImageRef' (aka 'struct CGImage *')

How can I assign a CGImageRef to the contents property when it only takes an id? What am I missing here?


回答1:


Explicitly casting CGImageRef to id should fix the warning. For example,

CGImageRef imageRef;

...

layer.contents = (id) imageRef;

should be working fine.




回答2:


Instead of using (id) you can use (__bridge id) to solve the type convert issue.



来源:https://stackoverflow.com/questions/5455080/how-do-i-set-the-contents-of-a-calayer-to-a-cgimageref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!