Git Product home page Git Product logo

Comments (10)

nicklockwood avatar nicklockwood commented on July 20, 2024

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.

nicklockwood avatar nicklockwood commented on July 20, 2024

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.

Thel22 avatar Thel22 commented on July 20, 2024

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.

Thel22 avatar Thel22 commented on July 20, 2024

Worked perfectly, thanks so much Nick!

from asyncimageview.

Thel22 avatar Thel22 commented on July 20, 2024

Oh, and I noticed that you have a donation section on your website. I will be contributing soon :)

from asyncimageview.

Thel22 avatar Thel22 commented on July 20, 2024

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.

nicklockwood avatar nicklockwood commented on July 20, 2024

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.

Thel22 avatar Thel22 commented on July 20, 2024

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.

nicklockwood avatar nicklockwood commented on July 20, 2024

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:

  1. 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.

  2. Display a UIActivityView in front of your tableView just before you download the images plist, and hide it again when you aet the array.

  3. 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.

Thel22 avatar Thel22 commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.