问题描述
我正在使用
[NSData dataWithContentsOfURL:imageUrl]
要为我的 tableview 单元格设置自定义高度,我需要正在加载的图像的实际大小.
For setting custom height for my tableview cell , i need the actual size of the image that am loading.
我们可以在完全加载之前获取图像的宽度和高度吗?提前致谢.
Can we get the width and height of an image before loading it completely ? Thanks in advance.
推荐答案
NSData
是一种不透明"数据,因此在将其转换为更有用"的东西(例如,通过它的 -initWithData:
方法创建一个 UIImage
).那个时候你可以查询图片的大小,但对你来说就晚了.
NSData
is an "opaque" data, so you cannot do much with it before converting it to something more "useful" (e.g., creating an UIImage
by means of it -initWithData:
method). At that moment you could enquiry the image size, but it would be late for you.
我看到的唯一方法是,如果您真的需要在图像完全下载之前知道图像大小,那就是实现最小的服务器端 API,以便您可以在尝试下载图像之前询问图像大小.
The only approach I see, if you really need knowing the image size before the image is fully downloaded, is implementing a minimal server-side API so that you can ask for the image size before trying to download it.
无论如何,为什么在实际下载之前需要知道图像大小?你能不能在下载的那一刻设置行高(即,从你的请求委托方法中)?
Anyway, why do you need to know the image size before it is actually downloaded? Could you not set the row height at the moment when it has been downloaded (i.e., from your request delegate method)?
这篇关于在将图像完全加载到 iPhone 之前获取图像的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!