SDWebImage load image then receive memory warning and crash - ios

I use SDWebImage in my app. I have several UIImageView in my viewcontroller, and
all of them use setImageWithURL: method(in UIImageView+WebCache.m)
load image by URL. There are three images should be load, two images
about 520kb, and the little one is 180kb. While I do run it , it will
receive memory warning and crash, I have no idea about it. Can anyone
help me, thanks!
Here is my code:
NSDictionary *data = [respDic objectForKey:#"data"];
[self.topLeft setImageWithURL:[NSURL URLWithString:[data valueForKey:#"tr_cu_1"]]];
[self.bottomLeft setImageWithURL:[NSURL URLWithString:[data valueForKey:#"tr_cu_2"]]];
[self.bottomRight setImageWithURL:[NSURL URLWithString:[data valueForKey:#"tr_cu_3"]]];

Related

How to use SDWebImage in my project

Iam using collectionview to load image.These images are loaded from url in background thread. Hence the scrolling is not smooth.The images in url get expired after seconds hence i want to update the image with new image.How can i use the SDWebImage to load image and make scrolling smooth? Can anyone please help me to solve this.
Use this code
[YOUR_CELL_NAME.YOUR_IMAGE_NAME setImageWithURL:[NSURL URLWithString:#"IMAGE URL"] placeholderImage:[UIImage imageNamed:#"PLACEHOLDER IMAGE NAME"]];
You need to integrate the sdwebimage classes in your code. After that make a import.
Download sdwebimage from here
#import "UIImageView+WebCache.h"
and in your collectionview method write following lines to download image in backend thread.
[YOUR_CELL_NAME.YOUR_IMAGE_NAME setImageWithURL:[NSURL URLWithString:#"IMAGE URL"] placeholderImage:[UIImage imageNamed:#"PLACEHOLDER IMAGE NAME"]];

iOS UITableViewCell Images from URL

I am loading images in my UITableViewCell from some web url and those images are in size around 80-100 kb per image. Everything works fine when i download images asynchronously but when i scroll the tableview, images start again downloading. Can i get rid of this as i have only limited no of rows.
I want if images from url download once should not be download again as it does makes view not comfortable for users when images starts downloading again.
I am using below code to load images in CellforRowAtIndexPath:
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:url];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
cell.eventImage.image= [UIImage imageWithData:data];
});
});
You should use SDWebImage which will cache image on your device and if same image came from server then it load from your local device.
Add SDWebImage to your project.
import <SDWebImage/UIImageView+WebCache.h> header
then add below code at cellForItemAtIndexPath:
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:#"your image URL"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Better make use of SDWebImageCache for this because this a advantageous third party library to store the image in cache. After downloading once the images will be taken automatically from the cache.
SDWebImageCache
When you Download image put it to the document directory and before you download image check if document directory contains image then load it from there otherwise download from URL
If you want you can make it yourself. You can use NSCache in order to save your images. NSCache it is like NSMutableDictionary (save your image by key). And then you can check it. For key you can use path to your image.
But I also recommend you to use SDWebImage.

How to get the image from in memory cache using SDWebImage?

I have implemented SDWebImage in cellforrowatindexpath method. The problem here is when i scroll down the tableview the images are loading from web which is expected but when i scroll up the tableview the images are again downloading. What is the way to get rid of this problem. I don't want to load images from url overtime (since there is a usage limit with the web service call). I chose to use SDWebImage API for in memory cache so that once the images are loaded they are taken from cache to show it on the UI instead of making the web service call everytime. Here is my code:
[cell.placeImage sd_setImageWithURL:url placeholderImage:nil options:SDWebImageCacheMemoryOnly];
The above line is the only code in that method. Please help me.
Any kind of help is appreciated. I have seen other questions in this community but nobody said how to get an image from the cache memory.
For refreshing of the images you should use option : SDWebImageRefreshCached
[imageView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageRefreshCached];

Why UIImageview did not showing url image in Xcode 7?

I am working on iOS application using new version Xcode 7 and iOS 9. I am facing the following issue.
UIImageView is not showing images coming through URL. I am using the following code but imageview did not show images.
NSURL *url = [NSURL URLWithString:#"http://XXX.XX.XX.XXX:XXXX/XXXXXXXX/7339bec316b7404299c582f5904a6d3f.jpeg"];
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data ];
imageview.image=img;
I don't know what is the issue in Xcode 7, but it is working previous version.
Thanks in advance,
The above code should work for synchronous loading of image into ImageView. Make sure URL and imageView code is right. Alternatively you can use SDWebImage for loading of images into imageView. It has Asynchronous loading, caching and many more options like post response processing which are very useful. It's very simple to implement too. Sample code is below.
#import <SDWebImage/UIImageView+WebCache.h>
[_profilePic sd_setImageWithURL:[NSURL URLWithString:imageUrl]
placeholderImage:[UIImage imageNamed:#"placeholderIcon"]];
This will populate your imageView with a placeholder and when the image is fetched it replaces placeholder with it. Check out the github project for more options.

SDImageCache not working

I am encountering a very strange issue in my application.
I have used SDWebImage in the application, to download the images to cache, when network present and then retrieve the images from the cache, when absent.
The SDWebImage works perfectly in case of UITableViews. One can even see the stored/cached image files in the iPhone Simulator folder.
And then theres this one piece of code, that is troubling me.
I am displaying a simple imageview, and caching the image from the network.
The image is displayed properly, when network is present.
But in case of no connectivity, nothing is displayed.
Am assuming, that the caching is not taking place.
Lines of code
dispatch_async(dispatch_get_main_queue(), ^{
[im setImageWithURL:[NSURL URLWithString:[[NSString stringWithFormat:#"%#%#",kImgaePath,[[marrresultingArraySet objectAtIndex:0] objectAtIndex:kIndex_RecipeImage_Local]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:#"no_imagepng.jpg"]];});

Resources