How? UITableViewCell with UIImageView asynchronously loaded via ASINetworkQueue

前端 未结 1 913
走了就别回头了
走了就别回头了 2020-12-13 07:33

I\'m trying to load some images in table cells asynchronously using ASINetworkQueue. I just can\'t figure it out and can\'t seem to find a good SIMPLE example.

相关标签:
1条回答
  • 2020-12-13 08:34

    Here's a class derived from UIImageView which I use, perhaps this will help you. (Actually I've simplified this a fair bit from what I use, but that was what you asked for!)

    Header file, UIHTTPImageView.h:

    #import "ASIHTTPRequest.h"
    
    @interface UIHTTPImageView : UIImageView {
        ASIHTTPRequest *request;
    }
    
    - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
    
    @end
    

    and UIHTTPImageView.m:

    #import "UIHTTPImageView.h"
    
    @implementation UIHTTPImageView        
    
    - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
        [request setDelegate:nil];
        [request cancel];
        [request release];
    
        request = [[ASIHTTPRequest requestWithURL:url] retain];
        [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
    
        if (placeholder)
            self.image = placeholder;
    
        [request setDelegate:self];
        [request startAsynchronous];
    }
    
    - (void)dealloc {
        [request setDelegate:nil];
        [request cancel];
        [request release];
        [super dealloc];
    }
    
    - (void)requestFinished:(ASIHTTPRequest *)req
    {
    
        if (request.responseStatusCode != 200)
            return;
    
        self.image = [UIImage imageWithData:request.responseData];
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题