mayoff / uiimage-from-animated-gif

A UIImage category that loads animated GIFs
Creative Commons Zero v1.0 Universal
877 stars 164 forks source link

This isnt asynchronous :( #4

Closed sankaet closed 11 years ago

sankaet commented 11 years ago

I am trying to use this in a uitableview and parsing the links from the web. but the problem is that this library is not Asynchronous. Any future plans to support that?

mayoff commented 11 years ago

If you need asynchronicity, call this category's methods from dispatch_async or an NSBlockOperation.

sankaet commented 11 years ago

mayoff, its not just that. I am using dispatch_async to cache all the image data first and then loading it into the Imageview, but still the scrolling stops for a second before it goes to the next cell. I hope I am making sense.

When I scroll down and the image is in the cache and is ready to load with postGif.image = [UIImage animatedImageWithAnimatedGIFData:imageData];

then the scrolling halts for a second before it scrolls down, can you please suggest a fix for it?

mayoff commented 11 years ago

Are you performing postGif.image = [UIImage animatedImageWithAnimatedGIFData:imageData]; on a background queue? You need to create the instance of UIImage off the main queue.

sankaet commented 11 years ago

mayoff, this is what I am doing

postGif = (UIImageView *)[cell viewWithTag:104];

if ([[ImageCache sharedImageCache] DoesExist:post[@"gif"]] == true)
{
    image = [[ImageCache sharedImageCache] GetImage:post[@"gif"]];

    postGif.image = [UIImage animatedImageWithAnimatedGIFData:image];

}else
{
    dispatch_queue_t backgroundQueue = dispatch_queue_create("cacheImage", 0);

    dispatch_async(backgroundQueue, ^{

        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:post[@"gif"]]];
        dispatch_async(dispatch_get_main_queue(), ^{

            // Add the image to the cache
            [[ImageCache sharedImageCache] AddImage:post[@"gif"] image:imageData];

            NSIndexPath* ind = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
            [self.feedTableView beginUpdates];
            [self.feedTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:ind] withRowAnimation:UITableViewRowAnimationNone];
            [self.feedTableView endUpdates];
        });
    });
}

postGif.layer.cornerRadius = 2.0;
postGif.layer.masksToBounds = YES;

[cell.contentView addSubview:postGif];
mayoff commented 11 years ago

So, in other words, no. You are not doing what I just said you need to do: you need to create the instance of UIImage on a background queue.

sankaet commented 11 years ago

mayoff I tried background queue as well. but the Image load is really slow with it. Am I doing something wrong in this?

if ([[ImageCache sharedImageCache] DoesExist:post[@"gif"]] == true)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                             (unsigned long)NULL), ^(void) {
        NSData *image = [[ImageCache sharedImageCache] GetImage:post[@"gif"]];

        postGif.image = [UIImage animatedImageWithAnimatedGIFData:image];
    });

}else
{
    dispatch_queue_t backgroundQueue = dispatch_queue_create("cacheImage", 0);

    dispatch_async(backgroundQueue, ^{

        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:post[@"gif"]]];
        dispatch_async(dispatch_get_main_queue(), ^{

            // Add the image to the cache
            [[ImageCache sharedImageCache] AddImage:post[@"gif"] image:imageData];

            NSIndexPath* ind = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
            [self.feedTableView beginUpdates];
            [self.feedTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:ind] withRowAnimation:UITableViewRowAnimationNone];
            [self.feedTableView endUpdates];
        });
    });
}
mayoff commented 11 years ago

You should not modify properties of user interface objects (like a UIImageView) from a background queue. If you need more help, please go to http://stackoverflow.com and post your question there.

sankaet commented 11 years ago

mayoff, I posted a question here

http://stackoverflow.com/questions/16313585/uiimageview-scrolling-halts-for-a-second

I am sorry for bothering you again. I am just not grasping a concept I guess and thats putting me to a halt. I really appreciate your help!