andreylunevich / dlimageloader-ios Goto Github PK
View Code? Open in Web Editor NEWImage Loader for iOS on swift and objective-c languages
License: Other
Image Loader for iOS on swift and objective-c languages
License: Other
Hey Andrei what's up!
As you know I'm a huge fan always promoting DL around .. http://stackoverflow.com/a/26117923/294884
It is incredibly important to handle "skimming" when going through a list.
In short you have to - quite simply - add a delay before doing anything. I explain here http://stackoverflow.com/a/26034382/294884
Please consider this code,...
-(void)loadImageInASecIfItsTheSameAs:(NSString *)urlWasThen
{
NSData *imageImmediateData = [DLImageLoader imageIfInCacheAlready:urlWasThen];
if ( imageImmediateData != nil )
{
UIImage *imageImmediate = [UIImage imageWithData:imageImmediateData];
self.cellImage.image = imageImmediate;
return;
}
dispatch_after_secs_on_main(0.4, ^
{
if ( ! [urlWasThen isEqualToString:self.currentImage] )
return;
[DLImageLoader loadImageFromURL:urlWasThen
completed:^(NSError *error, NSData *imgData)
{
if (self == nil) return;
if ( ! [urlWasThen isEqualToString:self.currentImage] )
return;
UIImage *image = [UIImage imageWithData:imgData];
self.cellImage.image = image;
}];
});
}
Notice I had to add one new function to DLImageLoder -- like this ..
+(NSData *)imageIfInCacheAlready:(NSString *)urlString
{
NSData *imageOrNil =
[[DLILCacheManager sharedInstance] imageFromCache:urlString];
return imageOrNil;
}
Note the point is I get a Nil if the image IS NOT THERE -- then I know to handle it differently.
t first I had a call like "isImageAlreadyThere" ... in fact maybe it's better to do that separately?
Andrei, what do you think of this? Is there a better way?
Have you already covered this in a newer version?
Is my code OK?
Thanks!!
Below I include my routine above WITH LONG COMMENTS, it may help someone.
-(void)loadImageInASecIfItsTheSameAs:(NSString *)urlWasThen
{
// first, the image may be already on-hand in the cache...
// in other words -- the user may be scrolling up and down and they've simply returned to parts of the table
// where the image is already loaded. in that case, of course simply instantly load the image:
NSData *imageImmediateData = [DLImageLoader imageIfInCacheAlready:urlWasThen];
if ( imageImmediateData != nil )
{
UIImage *imageImmediate = [UIImage imageWithData:imageImmediateData];
//NSLog(@" +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ insta-image! ");
self.cellImage.image = imageImmediate;
return;
}
// if not, we must be careful to "not load images while skimming"
// SIMPLY WAIT A SHORT TIME........
dispatch_after_secs_on_main(0.4, ^
{
if ( ! [urlWasThen isEqualToString:self.currentImage] )
{
// so in other words, in fact, after a short period of time,
// the user has indeed scrolled away from that item. (ie, the user is skimming)
// this item is now some "new" item so of course we don't bother loading "that old" item
// ie, we now know the user was simply skimming over that item.
// (just TBC in the preliminary clause above, since the image is already in cache,
// we'd just instantly load the image - even if the user is skimming)
//NSLog(@" --- --- --- --- --- --- --- --- --- --- too quick!");
return;
}
// a short time has passed, and indeed this cell is still "that" item
// the user is not skimming, so we start loading the image.
//NSLog(@" --- not too quick ");
[DLImageLoader loadImageFromURL:urlWasThen completed:^(NSError *error, NSData *imgData)
{
if (self == nil) return;
// some time has passed while the image was loading from the internet...
if ( ! [urlWasThen isEqualToString:self.currentImage] )
{
// in this case, not due to skimming, but because os much time has passed,
// the user has moved on to some other part of the table.
// we pointlessly loaded the image from the internet! doh!
//NSLog(@" === === === === === === === === 'too late!' image load!");
return;
}
UIImage *image = [UIImage imageWithData:imgData];
self.cellImage.image = image;
}];
});
}
How to do cancel cache?
Are there plans for a swift 2 release?
Wondering if your code can work on something like the following? If yes, could you explain?
SCTableViewSection *Section3 = [self.tableViewModel sectionAtIndex:0];
Section3.cellActions.lazyLoad = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
{
DLImageView *imageView = (DLImageView *)[cell viewWithTag:1];
NSString *imageString = (NSString *)[cell.boundObject valueForKey:@"thumbnail"];
NSString *fullName = [NSString stringWithFormat:@"Documents/%@", imageString];
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:fullName];
UIImage *originalImage = [UIImage imageWithContentsOfFile:imagePath];
NSLog(@"added image from lazyload");
// size of actual thumbnail to display in cell
CGSize destinationSize = CGSizeMake(154,95); // size of actual thumbnail
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[UIView transitionWithView:imageView
duration:2.25f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
imageView.image = thumbnail;
} completion:nil];
};
While scrolling through a table view, currently DLImageLoader continues to load older requests and views them on reused table cells.
As stopping all operations is not acceptable, could you implement a method to cancel a single operation?
Something like...
- (void)stopDataLoadingWithURL:(NSString *)url
{
for ( DLILOperation *operation in self.operationQueue.operations ) {
if ( [operation.url isEqualToString:url] ) {
[operation cancelLoading];
break;
}
}
}
I tried to implement this on my own, however I was not successful. Operations seems to be empty evertime I tried to call the method.
hi there
please add your lib to cocoapods. i really like it
thank you.
Hello, nowhere is mentioned the license for this code. I'd like to use it within an MIT licensed plugin for PhoneGap. Is it possible?
Just got my app submit rejected by 2.23 Guideline (Apps must follow the iOS Data Storage Guidelines or they will be rejected).
Im wondering, by default where does imageFromUrl stores image cache? Im using:
[[DLImageLoader sharedInstance] imageFromUrl:[NSString stringWithFormat:@"http://159.203.67.55/4kidz/php_api/public/%@",laMemoria.imagen]
completed:^(NSError *error, UIImage *image) {
if (error == nil) {
// if we have no any errors
laMemoria.laImagen=[self addBorderToImage:image];
laMemoria.imagenLimpia=image;
dispatch_async(dispatch_get_main_queue(), ^{
[self.tabla reloadData];
});
} else {
// if we got an error when load an image
}
}];
Could this method addSkipBackupAttributeToItemAtPath be a solution? Passing the filepath:
- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
{
NSURL* URL= [NSURL fileURLWithPath: filePathString];
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
}
return success;
}
Thanks for the help
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.