访问 UIImage 属性而不将图像加载到内存中
如您所知,iPhone 指南不鼓励加载大于 1024x1024 的 UIImage
。
我需要加载的图像的大小各不相同,我想检查我要加载的图像的大小;但是,使用 UIImage
的 .size
属性需要加载图像......这正是我想要避免的。
我的推理有问题还是有解决办法?
As you know the iPhone guidelines discourage loading UIImage
s that are greater than 1024x1024.
The size of the images that I would have to load varies, and I would like to check the size of the image I am about to load; however using the .size
property of UIImage
requires the image to be loaded... which is exactly what I am trying to avoid.
Is there something wrong in my reasoning or is there a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 iOS 4.0 开始,iOS SDK 包含
CGImageSource。 ..
函数(在 ImageIO 框架中)。这是一个非常灵活的 API,可以查询元数据,而无需将图像加载到内存中。获取图像的像素尺寸应该像这样工作(确保在您的目标中包含 ImageIO.framework):(改编自 Gelphman 和 Laden 的“Programming with Quartz”,清单 9.5,第 228 页)
As of iOS 4.0, the iOS SDK includes the
CGImageSource...
functions (in the ImageIO framework). It's a very flexible API to query metadata without loading the image into memory. Getting the pixel dimensions of an image should work like this (make sure to include the ImageIO.framework in your target):(adapted from "Programming with Quartz" by Gelphman and Laden, listing 9.5, page 228)
Swift 3 版本的答案:
Swift 3 version of the answer:
在 Swift 5 中,对于
ImageIO
,imageProperties[kCGImagePropertyOrientation as String]
可能为零。它为零,我用
png
图像文件进行了测试,如 Apple 所说
In Swift 5, with
ImageIO
,imageProperties[kCGImagePropertyOrientation as String]
may be nil.It is nil, I tested with
png
image fileas Apple says
仅供记录...
我已经尝试过:
对于存储在我的内部 SSD 上的大约 300k 图像,这需要 1 分钟。
这样就可以了。
然而! ..如果在存储在外部硬盘上的文件夹上执行,我会得到以下计时:
所有实验均在同一硬盘、WD MyPassport Ultra、1 TB、连接至 Macbook Air M2 的 USB-A 连接器上的不同文件夹上进行。
具有相同文件数/GB 的时间分别是相同的文件夹。
(*):这些是我在循环中添加以下几行的时间:(
加上一些错误检查,我在这里省略。)
首先,我们可以看到绝大多数时间都花在 CGImageSourceCreateWithURL() 上。
其次,似乎有一些缓存效果,虽然我很难理解这一点,但这不是重点。
第三,持续时间不是线性的;我想这可能也与文件的大小有关,但同样,没有进一步调查。
所以,在我看来 CGImageSourceCreateWithURL() 确实加载了完整的图像文件。
我不明白为什么 Ole Bergmann 可以声称他的方法没有加载整个图像。
Just for the record ...
I have tried this:
This takes 1 minute for around 300k images stored on my internal SSD.
That would be OK.
However! .. if performed on a folder stored on an external hard disk, I get the following timings:
All experiments were done on different folders on the same hard disk, WD MyPassport Ultra, 1 TB, USB-A connector to Macbook Air M2.
Timings with the same number of files/GB were the same folders, resp.
(*): these were timings where I added the following lines to the loop:
(Plus some error checking, which I omit here.)
First of all, we can see that the vast majority of time is spent on CGImageSourceCreateWithURL().
Second, there seem to be some caching effects, although I have a hard time understanding that, but that is not the point.
Third, the durations are not linear; I guess it might have something to do with the sizes of the files, too, but again, didn't investigate further.
So, it looks to me like CGImageSourceCreateWithURL() really loads the complete image file.
I don't see why Ole Bergmann can claim his approach does not load the whole image.