创建其中包含图像的 CALayer 最有效的方法是什么?

发布于 2024-07-24 01:02:53 字数 122 浏览 8 评论 0原文

我想知道最有效的方法是制作一个带有图像的 CALayer。

我知道你可以加载 UIImage 然后调用 [image CGImage] 但这是最​​好的方法吗? 据我从苹果的文档中可以看出,这是唯一的方法。

I am wondering what the most efficient way is to make a CALayer with an image in it.

I know you can load a UIImage and then call [image CGImage] but is this the best way of going about it? As far as I can tell from Apple's documentation this is the only way you can do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

宁愿没拥抱 2024-07-31 01:02:53

试试这个:

layer.contents = (__bridge id)[ uiimage CGImage ] ;

HTH

Try this:

layer.contents = (__bridge id)[ uiimage CGImage ] ;

HTH

陌上青苔 2024-07-31 01:02:53

好吧,CGImage 不是CALayer,所以你显然留下了一些步骤,但我假设你知道你在说什么,就绘图而言到 CALayer 或其他什么。

如果您的问题是关于在不使用 UIImage 的情况下创建 CGImage 的问题,您可以尝试查看以下函数:

  • CGImageCreateWithJPEGDataProvider
  • CGImageCreateWithPNGDataProvider

如果您事先知道它是什么类型的图像,则只需使用适当的方法即可。 否则,您需要查看文件签名以查看它是否包含 PNG 或 JFIF。 当然,这需要您实现一个CGDataProvider

我认为这正是 UIImage 类已经在做的事情。 如果您想从中充分发挥效率,可以在 UIImage 类中使用以下方法之一:

+ (UIImage *)imageWithContentsOfFile:(NSString *)path
+ (UIImage *)imageWithData:(NSData *)data

这些方法之间的唯一区别

+ (UIImage *)imageNamed:(NSString *)name

imageNamed: 缓存图像。 如果您使用第一种方法,则不会。

Well, a CGImage isn't a CALayer, so you are obviously leaving some steps out there, but I assume you know what you're talking about, as far as drawing to a CALayer or whatnot.

If your question is about creating CGImages without using a UIImage, you can try looking into the following functions:

  • CGImageCreateWithJPEGDataProvider
  • CGImageCreateWithPNGDataProvider

If you happen to know beforehand what sort of image it is, just use the appropriate method. Otherwise, you'd need to look at the file signature to see if it contains PNG or JFIF. Of course, this requires you implement a CGDataProvider.

I assume this is exactly what the UIImage class is already doing. If you want to squeeze out every bit of efficiency from that, you can use one of the following methods in the UIImage class:

+ (UIImage *)imageWithContentsOfFile:(NSString *)path
+ (UIImage *)imageWithData:(NSData *)data

The only difference between these methods and

+ (UIImage *)imageNamed:(NSString *)name

is that imageNamed: caches the image. If you use one of the first methods, it does not.

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