在CALayer中使用UIImage会有性能影响吗?

时间:2023-02-10 16:56:03

I'm using a whole bunch of CALayers, creating a tile-based image not unlike GoogleMaps (different versions of the same image with more/less detail).

我正在使用一大堆CALayers,创建一个基于图块的图像,与GoogleMaps不同(同一图像的不同版本具有更多/更少的细节)。

The code I'm using to do this is:

我用来做这个的代码是:

UIImage* image = [self loadImage:obj.fileName zoomLevel:obj.zoomLevel];
[CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
obj.layerToAddTo.contents = [image CGImage];
[CATransaction commit];

I don't really feel like loading the CGImage from file using CoreGraphics because I'm lazy. But I will if there's a big performance boost! LoadImage just mangles a string to get the right path for loading said image, and obj is a NSObject-struct that holds all the info I need for this thread.

我真的不想使用CoreGraphics从文件加载CGImage,因为我很懒。但是,如果有很大的性能提升,我会的! LoadImage只是破坏了一个字符串以获得加载所述图像的正确路径,而obj是一个NSObject-struct,它保存了该线程所需的所有信息。

Help?

5 个解决方案

#1


3  

There's not a big performance boost - if anything it's the other way around. By going throuh UIImage to load up your images, you'll get all the benefits of caching that it does for you and it'll be a very speedy critter to use with your various CALayers.

没有太大的性能提升 - 如果有的话,那就是另一种方式。通过UIImage加载你的图像,你将获得它为你做的缓存的所有好处,它将是一个非常快速的小动物与你的各种CALayers一起使用。

#2


1  

I just tried this and using pure CoreGraphics to load the image rather than using UIImage gave a noticeable speed improvement when loading many images in one go.

我只是尝试了这个并且使用纯CoreGraphics来加载图像而不是使用UIImage在一次加载许多图像时显着提高了速度。

#3


-1  

"I just tried this and using pure CoreGraphics to load the image rather than using UIImage gave a noticeable speed improvement when loading many images in one go."

“我只是尝试了这一点并使用纯CoreGraphics来加载图像而不是使用UIImage在一次加载许多图像时显着提高了速度。”

How did you avoid using a UIImage? Or more precisely, how do you load an image file directly into CoreGraphics without going through a UIImage?

你是如何避免使用UIImage的?或者更准确地说,如何在不通过UIImage的情况下将图像文件直接加载到CoreGraphics中?

#4


-1  

One reason why NOT to use UIImage -imageNamed: to load images is that they are stored in the internal cache and that cache is not cleared in low memory situation.

不使用UIImage -imageNamed:加载图像的一个原因是它们存储在内部缓存中,并且在低内存情况下不会清除该缓存。

#5


-3  

I don't have a definite answer but I'd guess that you'd see a slower load time when using UIImage than you'd see when using CGImage. With CGImage, you specify the image type (jpg or png) during creation, but with UIImage, the object type needs to be determined dynamically. Admittedly, this is probably as simple as looking at the first few bytes of the image file, but it might not be.

我没有明确的答案,但我猜你在使用UIImage时看到的加载时间比你在使用CGImage时看到的要慢。使用CGImage,您可以在创建过程中指定图像类型(jpg或png),但是使用UIImage时,需要动态确定对象类型。不可否认,这可能就像查看图像文件的前几个字节一样简单,但可能不是。

Once the image is actually in use, I wouldn't imagine that there'd be any difference at all between using the CGImage that internally represents a UIImage vs. using a CGImage you created yourself. I'd think they'd be exactly equivalent.

一旦图像实际使用,我不会想到使用内部表示UIImage的CGImage与使用您自己创建的CGImage之间存在任何差异。我认为他们完全相同。

#1


3  

There's not a big performance boost - if anything it's the other way around. By going throuh UIImage to load up your images, you'll get all the benefits of caching that it does for you and it'll be a very speedy critter to use with your various CALayers.

没有太大的性能提升 - 如果有的话,那就是另一种方式。通过UIImage加载你的图像,你将获得它为你做的缓存的所有好处,它将是一个非常快速的小动物与你的各种CALayers一起使用。

#2


1  

I just tried this and using pure CoreGraphics to load the image rather than using UIImage gave a noticeable speed improvement when loading many images in one go.

我只是尝试了这个并且使用纯CoreGraphics来加载图像而不是使用UIImage在一次加载许多图像时显着提高了速度。

#3


-1  

"I just tried this and using pure CoreGraphics to load the image rather than using UIImage gave a noticeable speed improvement when loading many images in one go."

“我只是尝试了这一点并使用纯CoreGraphics来加载图像而不是使用UIImage在一次加载许多图像时显着提高了速度。”

How did you avoid using a UIImage? Or more precisely, how do you load an image file directly into CoreGraphics without going through a UIImage?

你是如何避免使用UIImage的?或者更准确地说,如何在不通过UIImage的情况下将图像文件直接加载到CoreGraphics中?

#4


-1  

One reason why NOT to use UIImage -imageNamed: to load images is that they are stored in the internal cache and that cache is not cleared in low memory situation.

不使用UIImage -imageNamed:加载图像的一个原因是它们存储在内部缓存中,并且在低内存情况下不会清除该缓存。

#5


-3  

I don't have a definite answer but I'd guess that you'd see a slower load time when using UIImage than you'd see when using CGImage. With CGImage, you specify the image type (jpg or png) during creation, but with UIImage, the object type needs to be determined dynamically. Admittedly, this is probably as simple as looking at the first few bytes of the image file, but it might not be.

我没有明确的答案,但我猜你在使用UIImage时看到的加载时间比你在使用CGImage时看到的要慢。使用CGImage,您可以在创建过程中指定图像类型(jpg或png),但是使用UIImage时,需要动态确定对象类型。不可否认,这可能就像查看图像文件的前几个字节一样简单,但可能不是。

Once the image is actually in use, I wouldn't imagine that there'd be any difference at all between using the CGImage that internally represents a UIImage vs. using a CGImage you created yourself. I'd think they'd be exactly equivalent.

一旦图像实际使用,我不会想到使用内部表示UIImage的CGImage与使用您自己创建的CGImage之间存在任何差异。我认为他们完全相同。