Easy load images asynchronously in a UITableView using GCD - ios

After two days searching this answer I found a solution.
Use GCD for download images asynchronously.
Use NSMutableDictionary for save images in memory.
I found the solution explained here by Duncan C:
http://iphonedevsdk.com/forum/iphone-sdk-development/104438-grand-central-dispatch-tableview-images-from-the-web.html
How to implement:
- (void)viewDidLoad
{
(...)
dispatch_queue_t mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
imagesDictionary = [[NSMutableDictionary alloc] init];
(...)
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
(...)
NSData *imageData = [imagesDictionary objectForKey:#"IMAGE URL"];
if (imageData)
{
UIImage* image = [[UIImage alloc] initWithData:imageData];
imageFlag.image = image;
NSLog(#" Reatriving ImageData...: %#", #"IMAGE URL");
}
else
{
dispatch_async(mainQueue, ^(void) {
NSString *url = #"IMAGE URL";
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
imageFlag.image = image;
[imagesDictionary setObject:imageData forKey:#"IMAGE URL"];
NSLog(#" Downloading Image...: %#", #"IMAGE URL");
});
}
(...)
}
The project in GitHub: https://github.com/GabrielMassana/AsynchronousV2.git
I know that if the project is large and with a lot of cells I can go run out of memory. But I think this solution is a nice approach for a newbie.
What do you think about the project?
If the project is really large, the best option is to save the images in disk? But then the problem is the possibility to go run out of memory in the disk, isn't it? Maybe, then, we need a mechanism to delete all the images from disk.

Use NSCache to hold your downloaded images instead of NSDictionary. This will manage itself in low memory situations and remove items that haven't been accessed for a while. In that situation, they will be loaded from the URL again if needed.

Related

Which is the best way to cache the data from API's in swift? [duplicate]

Shortly, I have an NSDictionary with urls for images that I need to show in my UITableView. Each cell has a title and an image. I had successfully made this happen, although the scrolling was lagging, as it seemed like the cells downloaded their image every time they came into the screen.
I searched for a bit, and found SDWebImage on github. This made the scroll-lagg go away. I am not completely sure what it did, but I believed it did some caching.
But! Every time I open the app for the first time, I see NO images, and I have to scroll down, and back up for them to arrive. And if I exit the app with home-button, and open again, then it seemes like the caching is working, because the images on the screen are visible, however, if I scroll one cell down, then the next cell has no image. Until i scroll past it and back up, or if I click on it. Is this how caching is supposed to work? Or what is the best way to cache images downloaded from the web? The images are being updated rarily, so I was close to just import them to the project, but I like to have the possibility to update images without uploading an update..
Is it impossible to load all the images for the whole tableview form the cache(given that there is something in the cache) at launch? Is that why I sometimes see cells without images?
And yes, I'm having a hard time understanding what cache is.
--EDIT--
I tried this with only images of the same size (500x150), and the aspect-error is gone, however when I scroll up or down, there are images on all cells, but at first they are wrong. After the cell has been in the view for some milliseconds, the right image appears. This is amazingly annoying, but maybe how it has to be?.. It seemes like it chooses the wrong index from the cache at first. If I scroll slow, then I can see the images blink from wrong image to the correct one. If I scroll fast, then I believe the wrong images are visible at all times, but I can't tell due to the fast scrolling. When the fast scrolling slows down and eventually stops, the wrong images still appear, but immediately after it stops scrolling, it updates to the right images. I also have a custom UITableViewCell class, but I haven't made any big changes.. I haven't gone through my code very much yet, but I can't think of what may be wrong.. Maybe I have something in the wrong order.. I have programmed much in java, c#, php etc, but I'm having a hard time understanding Objective-c, with all the .h and .m ...
I have also `
#interface FirstViewController : UITableViewController{
/**/
NSCache *_imageCache;
}
(among other variables) in FirstViewController.h. Is this not correct?
Here's my cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"hallo";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSMutableArray *marr = [hallo objectAtIndex:indexPath.section];
NSDictionary *dict = [marr objectAtIndex:indexPath.row];
NSString* imageName = [dict objectForKey:#"Image"];
//NSLog(#"url: %#", imageURL);
UIImage *image = [_imageCache objectForKey:imageName];
if(image)
{
cell.imageView.image = image;
}
else
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString* imageURLString = [NSString stringWithFormat:#"example.com/%#", imageName];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageURL]];
if(image)
{
dispatch_async(dispatch_get_main_queue(), ^{
CustomCell *cell =(CustomCell*)[self.tableView cellForRowAtIndexPath:indexPath];
if(cell)
{
cell.imageView.image = image;
}
});
[_imageCache setObject:image forKey:imageName];
}
});
}
cell.textLabel.text = [dict objectForKey:#"Name"];
return cell;
}
Caching just means keeping a copy of the data that you need so that you don't have to load it from some slower source. For example, microprocessors often have cache memory where they keep copies of data so that they don't have to access RAM, which is a lot slower. Hard disks often have memory caches from which the file system can get much quicker access to blocks of data that have been accessed recently.
Similarly, if your app loads a lot of images from the network, it may be in your interest to cache them on your device instead of downloading them every time you need them. There are lots of ways to do that -- it sounds like you already found one. You might want to store the images you download in your app's /Library/Caches directory, especially if you don't expect them to change. Loading the images from secondary storage will be much, much quicker than loading them over the network.
You might also be interested in the little-known NSCache class for keeping the images you need in memory. NSCache works like a dictionary, but when memory gets tight it'll start releasing some of its contents. You can check the cache for a given image first, and if you don't find it there you can then look in your caches directory, and if you don't find it there you can download it. None of this will speed up image loading on your app the first time you run it, but once your app has downloaded most of what it needs it'll be much more responsive.
I think Caleb answered the caching question well. I was just going to touch upon the process for updating your UI as you retrieve images, e.g. assuming you have a NSCache for your images called _imageCache:
First, define an operation queue property for the tableview:
#property (nonatomic, strong) NSOperationQueue *queue;
Then in viewDidLoad, initialize this:
self.queue = [[NSOperationQueue alloc] init];
self.queue.maxConcurrentOperationCount = 4;
And then in cellForRowAtIndexPath, you could then:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ilvcCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// set the various cell properties
// now update the cell image
NSString *imagename = [self imageFilename:indexPath]; // the name of the image being retrieved
UIImage *image = [_imageCache objectForKey:imagename];
if (image)
{
// if we have an cachedImage sitting in memory already, then use it
cell.imageView.image = image;
}
else
{
cell.imageView.image = [UIImage imageNamed:#"blank_cell_image.png"];
// the get the image in the background
[self.queue addOperationWithBlock:^{
// get the UIImage
UIImage *image = [self getImage:imagename];
// if we found it, then update UI
if (image)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// if the cell is visible, then set the image
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell)
cell.imageView.image = image;
}];
[_imageCache setObject:image forKey:imagename];
}
}];
}
return cell;
}
I only mention this as I've seen a few code samples floating around on SO recently that use GCD to update the appropriate UIImageView image property, but in the process of dispatching the UI update back to the main queue, they employ curious techniques (e.g., reloading the cell or table, just updating the image property of the existing cell object returned at the top of the tableView:cellForRowAtIndexPath (which is a problem if the row has scrolled off the screen and the cell has been dequeued and is being reused for a new row), etc.). By using cellForRowAtIndexPath (not to be confused with tableView:cellForRowAtIndexPath), you can determine if the cell is still visible and/or if it may have scrolled off and been dequeued and reused.
The simplest solution is to go with something heavily used that has been stress tested.
SDWebImage is a powerful tool that helped me solve a similar problem and can easily be installed w/ cocoa pods. In podfile:
platform :ios, '6.1'
pod 'SDWebImage', '~>3.6'
Setup cache:
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:#"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
Cache image:
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
https://github.com/rs/SDWebImage
I think will be better for you user something like DLImageLoader.
More info -> https://github.com/AndreyLunevich/DLImageLoader-iOS
[[DLImageLoader sharedInstance] loadImageFromUrl:#"image_url_here"
completed:^(NSError *error, UIImage *image) {
if (error == nil) {
imageView.image = image;
} else {
// if we got an error when load an image
}
}];
For the part of the question about wrong images, it's because of the reuse of cells. Reuse of cells means that the existing cells, which go out of view (for example, the cells which go out of the screen in the top when you scroll towards the bottom are the ones coming back again from the bottom.) And so you get incorrect images. But once the cell shows up, the code for fetching the proper image executes and you get the proper images.
You can use a placeholder in 'prepareForReuse' method of the cell. This function is mostly used when you need to reset the values when the cell is brought up for reuse. Setting a placeholder here will make sure you won't get any incorrect images.
Caching images can be done as simply as this.
ImageService.m
#implementation ImageService{
NSCache * Cache;
}
const NSString * imageCacheKeyPrefix = #"Image-";
-(id) init {
self = [super init];
if(self) {
Cache = [[NSCache alloc] init];
}
return self;
}
/**
* Get Image from cache first and if not then get from server
*
**/
- (void) getImage: (NSString *) key
imagePath: (NSString *) imagePath
completion: (void (^)(UIImage * image)) handler
{
UIImage * image = [Cache objectForKey: key];
if( ! image || imagePath == nil || ! [imagePath length])
{
image = NOIMAGE; // Macro (UIImage*) for no image
[Cache setObject:image forKey: key];
dispatch_async(dispatch_get_main_queue(), ^(void){
handler(image);
});
}
else
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0ul ),^(void){
UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[imagePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
if( !image)
{
image = NOIMAGE;
}
[Cache setObject:image forKey: key];
dispatch_async(dispatch_get_main_queue(), ^(void){
handler(image);
});
});
}
}
- (void) getUserImage: (NSString *) userId
completion: (void (^)(UIImage * image)) handler
{
[self getImage: [NSString stringWithFormat: #"%#user-%#", imageCacheKeyPrefix, userId]
imagePath: [NSString stringWithFormat: #"http://graph.facebook.com/%#/picture?type=square", userId]
completion: handler];
}
SomeViewController.m
[imageService getUserImage: userId
completion: ^(UIImage *image) {
annotationImage.image = image;
}];
////.h file
#import <UIKit/UIKit.h>
#interface UIImageView (KJ_Imageview_WebCache)
-(void)loadImageUsingUrlString :(NSString *)urlString placeholder :(UIImage *)placeholder_image;
#end
//.m file
#import "UIImageView+KJ_Imageview_WebCache.h"
#implementation UIImageView (KJ_Imageview_WebCache)
-(void)loadImageUsingUrlString :(NSString *)urlString placeholder :(UIImage *)placeholder_image
{
NSString *imageUrlString = urlString;
NSURL *url = [NSURL URLWithString:urlString];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[self tream_char:urlString]];
NSLog(#"getImagePath--->%#",getImagePath);
UIImage *customImage = [UIImage imageWithContentsOfFile:getImagePath];
if (customImage)
{
self.image = customImage;
return;
}
else
{
self.image=placeholder_image;
}
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *uploadTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error)
{
NSLog(#"%#",[error localizedDescription]);
self.image=placeholder_image;
return ;
}
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *imageToCache = [UIImage imageWithData:data];
if (imageUrlString == urlString)
{
self.image = imageToCache;
}
[self saveImage:data ImageString:[self tream_char:urlString]];
});
}];
[uploadTask resume];
}
-(NSString *)tream_char :(NSString *)string
{
NSString *unfilteredString =string;
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:#"!##$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"] invertedSet];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:#""];
NSLog (#"Result: %#", resultString);
return resultString;
}
-(void)saveImage : (NSData *)Imagedata ImageString : (NSString *)imageString
{
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:imageString];
if (![Imagedata writeToFile:documentDirectoryFilename atomically:NO])
{
NSLog((#"Failed to cache image data to disk"));
}
else
{
NSLog(#"the cachedImagedPath is %#",documentDirectoryFilename);
}
}
#end
/// call
[cell.ProductImage loadImageUsingUrlString:[[ArrProductList objectAtIndex:indexPath.row] valueForKey:#"product_image"] placeholder:[UIImage imageNamed:#"app_placeholder"]];

How to store image data in cache

I have a mutable array of dictionary store image from url.So how to store image in application cache and i can view image when not have internet connection.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString * CellIndentifier=#"CustomCell";
CustomCell *cell = (CustomCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifier forIndexPath:indexPath];
NSDictionary *dictVideo = [self.videoList objectAtIndex:indexPath.row];
[cell.indicator startAnimating];
//set title
NSString *titleVideo = [dictVideo objectForKey:#"Title"];
[cell.myLabel setText:titleVideo];
// set image url
NSString *urlVideo = [dictVideo objectForKey:#"Url"];
NSURL *url = [NSURL URLWithString:urlVideo];
cell.imageView.image = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage * img = [[UIImage alloc]initWithData:data];
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(#"%ld %# %#",(long)indexPath.row,titleVideo,url);
[cell.imageView setImage: img];
[cell.indicator stopAnimating];
});
});
return cell;
}
you can use SDWebImage library as Umar Farooq said,Its support for both iOS 6 and 7
you can then use below code in your .m file
#import "UIImageView+WebCache.h"
UIImage *Noimg = [UIImage imageNamed:#"noimg.png"];
[cell.PlaceImg setImageWithURL:[NSURL URLWithString:strImgname] placeholderImage:Noimg];
Here Noimg is the placeholder image, its automatically manage by library when there is no image found on image location
NSURLCache already does this for you. Your application already has an implicit NSURLCache that may not have on disk persistence, so you want to explicitly set it:
NSURLCache *cache = [[NSURLCache alloc]initWithMemoryCapacity:(5 * 1024 * 1024) diskCapacity:(10 * 1024 *1024) diskPath:#"NSURLCache.db"];
[NSURLCache setSharedURLCache:cache];
And from that point on, the URL loading system will store data both in memory and on disk. This will allow it to be available when there is no connection. You continue to make requests as usual, they just return a lot faster when served out of the cache.
If you want to modify this behavior, you should modify the cache policy used by your NSURLRequest (for example, NSURLRequestReturnCacheDataElseLoad or NSURLRequestReturnCacheDataDontLoad). Unfortunately, you are using dataWithContentsOfURL: which does not provide an opportunity to do that. Using NSURLSessionTask or NSURLConnection would allow you to do so.
Keep in mind that the server does tell the client how long a response is valid for, and the default cache policy does obey that.

IOS Application Loading Data From Web

I am trying to load data from web with few simple steps:
NSData *JSONData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"******"]];
NSObject *json = [JSONData objectFromJSONData];
NSArray *arrayOfStreams = [json valueForKeyPath:#"programs"];
NSDictionary *stream = [arrayOfStreams objectAtIndex:0];
NSString *str = [[NSString alloc]initWithString:[stream valueForKey:#"image"]];
NSURL *urlForImage1 = [NSURL URLWithString:str];
NSData *imageData1 = [NSData dataWithContentsOfURL:urlForImage1];
_screenForVideo1.image = [UIImage imageWithData:imageData1];
But the problem is I am doing 30 of this right after my application launches...
I want to load about 5 of them, and than load others. Because when I try to load all of them at the same time, my app is not launching all of them loaded...
Is there any way that I can load first few of them, and wait, and than load others?
As for loading them, you should probably display a spinner, start loading the image in background and then replace the spinner with the image once it’s ready.
- (void) viewDidLoad {
UIActivityIndicator *spinner = …;
[self.view addSubview:spinner];
[self performSelectorInBackground:#selector(startLoadingImage)
withObject:nil];
}
- (void) startLoadingImage {
// You need an autorelease pool since you are running
// in a different thread now.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [UIImage imageNamed:#"foo"];
// All GUI updates have to be done from the main thread.
// We wait for the call to finish so that the pool won’t
// claim the image before imageDidFinishLoading: finishes.
[self performSelectorOnMainThread:#selector(imageDidFinishLoading:)
withObject:image waitUntilDone:YES];
[pool drain];
}
- (void) imageDidFinishLoading: (UIImage*) image {
// fade spinner out
// create UIImageView and fade in
}

Response Lag in UITableView displaying JSON data

In my application I am parsing JSON data and then displaying that data in UITableView. The information is displayed in the table but the touch response is lagging really bad. I did some research and found out that it is recommended to implement asynchronous loading for the information and especially the image, but I could not find any relevant solutio that worked with my JSON application. I would appreciate some suggestions and comments of how to sort out this problem, here is the code:
jURL Definition www.website.com/info.json
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(jQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
jURL];
[self performSelectorOnMainThread:#selector(fetchedData:)
withObject:data waitUntilDone:NO];
});
}
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* jsonDict = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
calRes = [jsonDict objectForKey:#"results"];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *calDict = [calRes objectAtIndex:indexPath.row];
NSURL *imageURL = [NSURL URLWithString:[calDict objectForKey:#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
cell.textLabel.text = [calDict objectForKey:#"name"];
cell.detailTextLabel.text = [calDict objectForKey:#"description"];
cell.imageView.image = imageLoad;
return cell;
}
I like to use the AFNetworking library for easy asynchronous image loading. You will need to include the library
#import "AFNetworking.h"
Then use it in the cellForRowAtIndexPath
[cell.imageView setImageWithURL:[NSURL URLWithString:[calDict objectForKey:#"image"]]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
You also need to supply a placeholder image. I use a blank JPG of the size you will want the final image will be.
You can use SDWebImage from this link : https://github.com/rs/SDWebImage
It is the simplest and fastest library for asynchronous image loading.
It also provides image caching.
You can just done the whole operations by simply calling this function:
[cell.imageView setImageWithURL:jURL
placeholderImage:[UIImage imageNamed:#"your place holder here"]];
Just enjoy it.
As a follow up, it will be good to cache the images locally using this very handy plugin by Jake Marsh: JMImageCache
This way, the images will not need to be loaded from the URL the next time you boot the app.
See, the lagging in cellforRowAtIndexPath is because of this
NSURL *imageURL = [NSURL URLWithString:[calDict objectForKey:#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *imageLoad = [[UIImage alloc] initWithData:imageData];
why don't you, use GCD for adding the image? Further more you can have your own NSCache to store the image so that every time the table reloads, the image can be directly loaded from the memory instead of firing the url.

Best way to cache images on ios app?

Shortly, I have an NSDictionary with urls for images that I need to show in my UITableView. Each cell has a title and an image. I had successfully made this happen, although the scrolling was lagging, as it seemed like the cells downloaded their image every time they came into the screen.
I searched for a bit, and found SDWebImage on github. This made the scroll-lagg go away. I am not completely sure what it did, but I believed it did some caching.
But! Every time I open the app for the first time, I see NO images, and I have to scroll down, and back up for them to arrive. And if I exit the app with home-button, and open again, then it seemes like the caching is working, because the images on the screen are visible, however, if I scroll one cell down, then the next cell has no image. Until i scroll past it and back up, or if I click on it. Is this how caching is supposed to work? Or what is the best way to cache images downloaded from the web? The images are being updated rarily, so I was close to just import them to the project, but I like to have the possibility to update images without uploading an update..
Is it impossible to load all the images for the whole tableview form the cache(given that there is something in the cache) at launch? Is that why I sometimes see cells without images?
And yes, I'm having a hard time understanding what cache is.
--EDIT--
I tried this with only images of the same size (500x150), and the aspect-error is gone, however when I scroll up or down, there are images on all cells, but at first they are wrong. After the cell has been in the view for some milliseconds, the right image appears. This is amazingly annoying, but maybe how it has to be?.. It seemes like it chooses the wrong index from the cache at first. If I scroll slow, then I can see the images blink from wrong image to the correct one. If I scroll fast, then I believe the wrong images are visible at all times, but I can't tell due to the fast scrolling. When the fast scrolling slows down and eventually stops, the wrong images still appear, but immediately after it stops scrolling, it updates to the right images. I also have a custom UITableViewCell class, but I haven't made any big changes.. I haven't gone through my code very much yet, but I can't think of what may be wrong.. Maybe I have something in the wrong order.. I have programmed much in java, c#, php etc, but I'm having a hard time understanding Objective-c, with all the .h and .m ...
I have also `
#interface FirstViewController : UITableViewController{
/**/
NSCache *_imageCache;
}
(among other variables) in FirstViewController.h. Is this not correct?
Here's my cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"hallo";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSMutableArray *marr = [hallo objectAtIndex:indexPath.section];
NSDictionary *dict = [marr objectAtIndex:indexPath.row];
NSString* imageName = [dict objectForKey:#"Image"];
//NSLog(#"url: %#", imageURL);
UIImage *image = [_imageCache objectForKey:imageName];
if(image)
{
cell.imageView.image = image;
}
else
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString* imageURLString = [NSString stringWithFormat:#"example.com/%#", imageName];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageURL]];
if(image)
{
dispatch_async(dispatch_get_main_queue(), ^{
CustomCell *cell =(CustomCell*)[self.tableView cellForRowAtIndexPath:indexPath];
if(cell)
{
cell.imageView.image = image;
}
});
[_imageCache setObject:image forKey:imageName];
}
});
}
cell.textLabel.text = [dict objectForKey:#"Name"];
return cell;
}
Caching just means keeping a copy of the data that you need so that you don't have to load it from some slower source. For example, microprocessors often have cache memory where they keep copies of data so that they don't have to access RAM, which is a lot slower. Hard disks often have memory caches from which the file system can get much quicker access to blocks of data that have been accessed recently.
Similarly, if your app loads a lot of images from the network, it may be in your interest to cache them on your device instead of downloading them every time you need them. There are lots of ways to do that -- it sounds like you already found one. You might want to store the images you download in your app's /Library/Caches directory, especially if you don't expect them to change. Loading the images from secondary storage will be much, much quicker than loading them over the network.
You might also be interested in the little-known NSCache class for keeping the images you need in memory. NSCache works like a dictionary, but when memory gets tight it'll start releasing some of its contents. You can check the cache for a given image first, and if you don't find it there you can then look in your caches directory, and if you don't find it there you can download it. None of this will speed up image loading on your app the first time you run it, but once your app has downloaded most of what it needs it'll be much more responsive.
I think Caleb answered the caching question well. I was just going to touch upon the process for updating your UI as you retrieve images, e.g. assuming you have a NSCache for your images called _imageCache:
First, define an operation queue property for the tableview:
#property (nonatomic, strong) NSOperationQueue *queue;
Then in viewDidLoad, initialize this:
self.queue = [[NSOperationQueue alloc] init];
self.queue.maxConcurrentOperationCount = 4;
And then in cellForRowAtIndexPath, you could then:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ilvcCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// set the various cell properties
// now update the cell image
NSString *imagename = [self imageFilename:indexPath]; // the name of the image being retrieved
UIImage *image = [_imageCache objectForKey:imagename];
if (image)
{
// if we have an cachedImage sitting in memory already, then use it
cell.imageView.image = image;
}
else
{
cell.imageView.image = [UIImage imageNamed:#"blank_cell_image.png"];
// the get the image in the background
[self.queue addOperationWithBlock:^{
// get the UIImage
UIImage *image = [self getImage:imagename];
// if we found it, then update UI
if (image)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// if the cell is visible, then set the image
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell)
cell.imageView.image = image;
}];
[_imageCache setObject:image forKey:imagename];
}
}];
}
return cell;
}
I only mention this as I've seen a few code samples floating around on SO recently that use GCD to update the appropriate UIImageView image property, but in the process of dispatching the UI update back to the main queue, they employ curious techniques (e.g., reloading the cell or table, just updating the image property of the existing cell object returned at the top of the tableView:cellForRowAtIndexPath (which is a problem if the row has scrolled off the screen and the cell has been dequeued and is being reused for a new row), etc.). By using cellForRowAtIndexPath (not to be confused with tableView:cellForRowAtIndexPath), you can determine if the cell is still visible and/or if it may have scrolled off and been dequeued and reused.
The simplest solution is to go with something heavily used that has been stress tested.
SDWebImage is a powerful tool that helped me solve a similar problem and can easily be installed w/ cocoa pods. In podfile:
platform :ios, '6.1'
pod 'SDWebImage', '~>3.6'
Setup cache:
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:#"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
Cache image:
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
https://github.com/rs/SDWebImage
I think will be better for you user something like DLImageLoader.
More info -> https://github.com/AndreyLunevich/DLImageLoader-iOS
[[DLImageLoader sharedInstance] loadImageFromUrl:#"image_url_here"
completed:^(NSError *error, UIImage *image) {
if (error == nil) {
imageView.image = image;
} else {
// if we got an error when load an image
}
}];
For the part of the question about wrong images, it's because of the reuse of cells. Reuse of cells means that the existing cells, which go out of view (for example, the cells which go out of the screen in the top when you scroll towards the bottom are the ones coming back again from the bottom.) And so you get incorrect images. But once the cell shows up, the code for fetching the proper image executes and you get the proper images.
You can use a placeholder in 'prepareForReuse' method of the cell. This function is mostly used when you need to reset the values when the cell is brought up for reuse. Setting a placeholder here will make sure you won't get any incorrect images.
Caching images can be done as simply as this.
ImageService.m
#implementation ImageService{
NSCache * Cache;
}
const NSString * imageCacheKeyPrefix = #"Image-";
-(id) init {
self = [super init];
if(self) {
Cache = [[NSCache alloc] init];
}
return self;
}
/**
* Get Image from cache first and if not then get from server
*
**/
- (void) getImage: (NSString *) key
imagePath: (NSString *) imagePath
completion: (void (^)(UIImage * image)) handler
{
UIImage * image = [Cache objectForKey: key];
if( ! image || imagePath == nil || ! [imagePath length])
{
image = NOIMAGE; // Macro (UIImage*) for no image
[Cache setObject:image forKey: key];
dispatch_async(dispatch_get_main_queue(), ^(void){
handler(image);
});
}
else
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0ul ),^(void){
UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[imagePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
if( !image)
{
image = NOIMAGE;
}
[Cache setObject:image forKey: key];
dispatch_async(dispatch_get_main_queue(), ^(void){
handler(image);
});
});
}
}
- (void) getUserImage: (NSString *) userId
completion: (void (^)(UIImage * image)) handler
{
[self getImage: [NSString stringWithFormat: #"%#user-%#", imageCacheKeyPrefix, userId]
imagePath: [NSString stringWithFormat: #"http://graph.facebook.com/%#/picture?type=square", userId]
completion: handler];
}
SomeViewController.m
[imageService getUserImage: userId
completion: ^(UIImage *image) {
annotationImage.image = image;
}];
////.h file
#import <UIKit/UIKit.h>
#interface UIImageView (KJ_Imageview_WebCache)
-(void)loadImageUsingUrlString :(NSString *)urlString placeholder :(UIImage *)placeholder_image;
#end
//.m file
#import "UIImageView+KJ_Imageview_WebCache.h"
#implementation UIImageView (KJ_Imageview_WebCache)
-(void)loadImageUsingUrlString :(NSString *)urlString placeholder :(UIImage *)placeholder_image
{
NSString *imageUrlString = urlString;
NSURL *url = [NSURL URLWithString:urlString];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[self tream_char:urlString]];
NSLog(#"getImagePath--->%#",getImagePath);
UIImage *customImage = [UIImage imageWithContentsOfFile:getImagePath];
if (customImage)
{
self.image = customImage;
return;
}
else
{
self.image=placeholder_image;
}
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *uploadTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error)
{
NSLog(#"%#",[error localizedDescription]);
self.image=placeholder_image;
return ;
}
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *imageToCache = [UIImage imageWithData:data];
if (imageUrlString == urlString)
{
self.image = imageToCache;
}
[self saveImage:data ImageString:[self tream_char:urlString]];
});
}];
[uploadTask resume];
}
-(NSString *)tream_char :(NSString *)string
{
NSString *unfilteredString =string;
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:#"!##$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"] invertedSet];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:#""];
NSLog (#"Result: %#", resultString);
return resultString;
}
-(void)saveImage : (NSData *)Imagedata ImageString : (NSString *)imageString
{
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:imageString];
if (![Imagedata writeToFile:documentDirectoryFilename atomically:NO])
{
NSLog((#"Failed to cache image data to disk"));
}
else
{
NSLog(#"the cachedImagedPath is %#",documentDirectoryFilename);
}
}
#end
/// call
[cell.ProductImage loadImageUsingUrlString:[[ArrProductList objectAtIndex:indexPath.row] valueForKey:#"product_image"] placeholder:[UIImage imageNamed:#"app_placeholder"]];

Resources