简体   繁体   English

使用SDWebImage处理非常大的图像下载

[英]Handling download of very large images with SDWebImage

I use SDWebImage to download images asynchronously to my UIImageView s. 我使用SDWebImage将图像异步下载到我的UIImageView Most images that received are of acceptable sizes, and can be easily downloaded, and set to the UIImageView s. 收到的大多数图像都是可接受的大小,可以轻松下载,并设置为UIImageView

However there are times, when the source image at the URL is of insanely high resolution (relative to the size of my imageView - 60x60). 但有时候,URL上的源图像分辨率非常高(相对于我的imageView - 60x60的大小)。 In such cases, the image is never set. 在这种情况下,永远不会设置图像。 Sometimes my app crashes, sometimes, nothing happens (image stays nil ), and in a very rare case I received an error something like : Unable to allocate 400000 bytes of memory (I am not entirely sure of the exact error log, I apologise). 有时我的应用程序崩溃,有时,没有任何反应(图像保持nil ),并且在极少数情况下我收到类似的错误: Unable to allocate 400000 bytes of memory (我不完全确定确切的错误日志,我道歉) 。

For example, this image of the pinterest icon from Pinterest's site itself is enormous (10000 x 10000). 例如, 从Pinterest的站点本身的Pinterest的图标的图像是巨大的(10000×10000)。 My imageView can never plot this image. 我的imageView永远无法绘制此图像。 For the time being, I have hardcoded to replace the image with this , but I know this is bad practice. 目前,我已经硬编码用这个替换图像,但我知道这是不好的做法。 Also, this is just one case, there might be infinitely more images such as this that might screw with the user experience. 此外,这只是一种情况,可能会有无限多的图像,这可能会影响用户体验。

How can I handle such cases ? 我该如何处理这类案件?

Just put a check for size of image that you receive and when size much greater than what your imageview would accept then you could resize the image ,and here link for resizing the image link . 只需检查您收到的图像大小,当大小远大于您的图像视图接受的大小时,您可以调整图像大小,此处链接用于调整图像链接的大小。 After resizing then you could place it in imageview. 调整大小后,您可以将其放在imageview中。 Hope helpful for you. 希望对你有所帮助。

I'd suggest you to use this AsyncImageView . 我建议你使用这个AsyncImageView I've used it and it work wonders. 我已经习惯了,它可以创造奇迹。 To call this API: 要调用此API:

ASyncImage *img_EventImag = alloc with frame;
NSURL *url = yourPhotoPath;
[img_EventImage loadImageFromURL:photoPath];
[self.view addSubView:img_EventImage];

It's same as using UIImageView. 它与使用UIImageView相同。 Easy and it does most of the things for you. 简单,它可以为您完成大部分工作。 AsyncImageView includes both a simple category on UIImageView for loading and displaying images asynchronously on iOS so that they do not lock up the UI, and a UIImageView subclass for more advanced features. AsyncImageView包含UIImageView上的一个简单类别,用于在iOS上异步加载和显示图像,以便它们不会锁定UI,以及用于更高级功能的UIImageView子类。 AsyncImageView works with URLs so it can be used with either local or remote files. AsyncImageView适用于URL,因此可以与本地或远程文件一起使用。

Loaded/downloaded images are cached in memory and are automatically cleaned up in the event of a memory warning. 加载/下载的图像缓存在内存中,并在发生内存警告时自动清除。 The AsyncImageView operates independently of the UIImage cache, but by default any images located in the root of the application bundle will be stored in the UIImage cache instead, avoiding any duplication of cached images. AsyncImageView独立于UIImage缓存运行,但默认情况下,位于应用程序包根目录中的任何图像都将存储在UIImage缓存中,从而避免重复缓存的图像。

The library can also be used to load and cache images independently of a UIImageView as it provides direct access to the underlying loading and caching classes. 该库还可用于独立于UIImageView加载和缓存图像,因为它提供对底层加载和缓存类的直接访问。

SDWebImage is open-source library. SDWebImage是开源库。 Add sources to your project and append your size-check in SDWebImageDownloader.m 将源添加到项目中并在SDWebImageDownloader.m中附加大小检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM