开发者

Async image downloader

开发者 https://www.devze.com 2023-01-22 02:57 出处:网络
I have write a little class to perform the download of the images by using an NSURLConnection. The idea it\'s to delegate the download to this class to avoid to block the execution.

I have write a little class to perform the download of the images by using an NSURLConnection. The idea it's to delegate the download to this class to avoid to block the execution.

So I pass the target UIImageView (by ref) and the url to the function and start the download:

-(void)getImage:(UIImageView**)image formUrl:(NSString*)urlStr
{
    NSLog(@"Downloader.getImage.url.debug: %@",urlStr);
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad t开发者_开发知识库imeoutInterval:5.0];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    [conn addObject:c];
    int i = [conn indexOfObject:c];
    [imgs insertObject:*image atIndex:i];
    [c release];
}

When it's finished set the image and update the imageView:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    int i = [conn indexOfObject:connection];

    NSData *rawData = [buffer objectAtIndex:i];
    UIImage *img = [UIImage imageWithData:rawData];
    UIImageView *imgView = [imgs objectAtIndex:i];
    imgView.image = img;

    [imgView setNeedsDisplay];

    [conn removeObjectAtIndex:i];
    [imgs removeObjectAtIndex:i];
    [buffer removeObjectAtIndex:i];

    if ([conn count]==0) {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    }
}

This system works quite good, but i can't get updated the UIImageView inside the cells of a UITableViewCell, any idea ? (actually the cell it's updated when I click on it) There is a better way to implement this functionality ? (actually the need are: non-blocking, caching system)


Cesar

For async image loading, caching and threaded operation, I use SDImageCache class from http://github.com/rs/SDWebImage. I also wrote my own several times, but this one is brilliant and I've thrown all my old code away.

From its documentation:

Just #import the UIImageView+WebCache.h header, and call the setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.

Hilton


Simply follow the steps:

    1) Download SDWebImage below  And drag to your xcode project. 

Click here to Download the SDWebImage

   2) Write the following code in your .h file #import "UIImageView+WebCache.h"
   3) Write the following code for your image view 
  [yourImageView setImageWithURL:[NSURL URLWithString:@"www.sample.com/image.png"]];

Thats its... you have done!!!


NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
    [img_data setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.str_url]]]];
    [pool release];


This system works quite good, but i can't get updated the UIImageView inside the cells of a UITableViewCell, any idea ? (actually the cell it's updated when I click on it) There is a better way to implement this functionality ? (actually the need are: non-blocking, caching system

Have you tried using [tableView reloadData]; Call it at the end of connectionDidFinishLoading method


Use SDWebImage simply. I'm also using that one. Loading images smoothy. Am using below code to show the image from URL

[myImageView setImageWithURL:[NSURL URLWithString:@"www.sample.com/image.png"] placeholderImage:[UIImage imageNamed:@"lod.png"] 
success:^(UIImage *image, BOOL cached)
{
    productImageDetails.image = image;
} 
failure:^(NSError *error) {
    productImageDetails.image =[UIImage imageNamed:@"no_image.jpg"];
}];

Success part will display the image from webService. And, if there's any image failed to load or anyother failure issue you can load your own image there simply. There are more functionalities with that example. You can just refer it.

0

精彩评论

暂无评论...
验证码 换一张
取 消