I use SDWebImage
to download images asynchronously to my UIImageView
s. Most images that received are of acceptable sizes, and can be easily downloaded, and set to the UIImageView
s.
However there are times, when the source image at the URL is of insanely high resolution (relative to the size of my 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).
For example, this image of the pinterest icon from Pinterest's site itself is enormous (10000 x 10000). My imageView
can never plot this image. 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. Hope helpful for you.
I'd suggest you to use this AsyncImageView. I've used it and it work wonders. To call this 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. 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 works with URLs so it can be used with either local or remote files.
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.
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.
SDWebImage is open-source library. Add sources to your project and append your size-check in SDWebImageDownloader.m