Comments (10)
Is the URL used in this line a remote URL?
NSDictionary *imagePaths = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"someURL"]];
If so it's probably that Plist file that is taking the 2-3 seconds to download, not the images themselves. dictionaryWithContentsOfURL should never be used for remote files as it blocks the main thread.
from asyncimageview.
You can do the plist download in the background a few different ways. The simplest is to use an async request library like my RequestQueue class (github.com/nicklockwood/RequestQueue), which you would use like this:
NSURL *URL = [NSURL URLWithString:@"someURL"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
[[RequestQueue mainQueue] addRequest:request completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data && !error)
{
NSPropertyListFormat format;
NSDictionary *imagePaths = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:&format error:&error];
//do something with your image paths
}
if (error)
{
NSLog(@"download error: %@", error);
}
}];
from asyncimageview.
Ah ok, that has to be it then. You are right, I was trying to access a plist remotely using dictionaryWithContentsOfURL.
Thank you for pointing me towards your RequestQueue example. I'll check it out and get back to you asap.
from asyncimageview.
Worked perfectly, thanks so much Nick!
from asyncimageview.
Oh, and I noticed that you have a donation section on your website. I will be contributing soon :)
from asyncimageview.
I tried using the code/example you provided at it worked great with the UIImageView. However, I'm having a little problem implementing it into a tableview. What's happening is that when first load the page it comes up blank, but if I switch views and go back (essentially using [tableview reloadData]) it pulls up the table. I commented out [tableview reloadData] to be certain and sure enough, the page stayed blank. Here's the code I'm trying to implement:
NSURL *URL = [NSURL URLWithString:@"someURL"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
[[RequestQueue mainQueue] addRequest:request completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (data && !error)
{
NSPropertyListFormat format;
NSDictionary *imagePaths = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:&format error:&error];
eventArray = [imagePaths objectForKey:@"eventCells"];
// eventArray being a NSMutableArray
NSMutableArray *URLs = [NSMutableArray array];
for (NSString *path in [imagePaths objectForKey:@"cellImage"])
{
NSURL *URL = [NSURL URLWithString:path];
if (URL)
{
[URLs addObject:URL];
}
else
{
NSLog(@"'%@' is not a valid URL", path);
}
}
self.imageURLs = URLs;
As far as I can tell it should be working. It is actually, just not when I first load the page. One thing I did notice, and is the reason it's not pulling up the data on the first try, is that "imageURLs" is always "nil" in numberOfRowsInSection the first time around.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [imageURLs count];
}
Again, any help is much appreciated. Thanks
from asyncimageview.
Call [tableView reloadData] immediately after self.imageURLs = URLs;
What's happening is that when your view first loads, the Plist hasn't been downloaded yet, so the tableView is calling numberOfRowsInSection
before the self.imageURLs array has been set, so you need to manually trigger the table reload again after the array has been set.
from asyncimageview.
Thanks Nick, that worked. Although the page initially shows blank then a second later all the images become visible. I would like the placeholder image to show immediately then have the images load but I guess in this case, since the table is being reloaded there's nothing I can do about it.
Thanks again.
from asyncimageview.
The problem you have is that because you are loading your list of images from a remote location, you don't know how many items to display in the table until that download is complete. You have a few options:
-
in your numberOfItemsInTableView you could return a hard - coded value of, say, 10 if the array is nil, and in your cellForItemAtIndexPath you could return jut a placeholder image if the array is nil. That way the table will still have placeholders while you're waiting.
-
Display a UIActivityView in front of your tableView just before you download the images plist, and hide it again when you aet the array.
-
since your images view controller isn't the first view that the user sees, why not download your image plist in the app delegate when the all first loads and then keep it. Your tableViewController can the access it from the app delegate, and there's a good chance it will already be downloaded by the time the user opens the table.
from asyncimageview.
Thanks for the suggestions. Unfortunately, since the table will be updated with new images on a weekly basis (maybe more some weeks) I'd like to avoid loading the plist in the appDelegate so the end users can see the updates "on the fly" so to speak.
I will be using a UIActivityView like you mentioned in 2). That works best for my situation, thanks!
from asyncimageview.
Related Issues (20)
- how to cancel all previous loading?
- Any swift version coming out soon? HOT 2
- Image loading problem with ImageIO error
- error: 'AsyncImageView' is ambiguous for type lookup in this context
- why not write to disk?
- AsyncImageLoadDidFinish doesnt get call if the image is cached HOT 3
- Does it support GIF?
- Crash: AsyncImageView.m line 340 -[AsyncImageLoader updateQueue] HOT 1
- How to show default iamge if the image is not loaded from server due to any error ?? HOT 2
- Can activityIndicator and placeholder image be used at the same time?
- AsyncImageView is not loading images in iOS 9.0 xcode 7+ HOT 2
- Trying to load Cached image when no internet is there, but shows blank. Is image cached and shows in Internet online mode only.
- Fatal Exception: NSInvalidArgumentException -[UIImageView setImageFailed:]: unrecognized selector sent to instance 0x127f2e860 HOT 3
- AsyncImageView.m line 175 -[AsyncImageConnection processDataInBackground:]
- How we can cache it permanently , Currently cache is clearing when come from background.
- Add Carthage support HOT 1
- AsyncImageView - [processDataInBackground:] returning an image with longer data bytes than the original HOT 1
- if Image Url is not valid
- If image url have space
- Update to swift 4.2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncimageview.