2013-03-08 86 views
0

我使用表格视图弗朗互联网和其采取的时间来加载图像显示图像..负荷图像的tableView

我试图

dispatch_async(imageQueue_, ^{ 
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[record imageURLString]]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [[cell imageView] setImage:[UIImage imageWithData:imageData]]; 
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
    }); 

但它的加载图像缓慢,重复,直到图像其他图像将加载在同一行.. 有没有办法在同一时间加载tableview中的所有图像(通常会发生一个一个..)?

+0

使用延迟加载显示图像 – Dhara 2013-03-08 09:14:03

+0

使用sdwebimage。 – Eugene 2013-03-08 09:15:06

+0

@Dhara:我没有得到如何使用懒加载.. – Raju 2013-03-08 09:35:01

回答

0

正如尤金提到的,使用SDWebImage。使这件事变得如此简单。

1

使2类AsyncImageView.h和.M

在AsyncImageView.h

@interface AsyncImageView : UIView 
{ 
    NSURLConnection *connection; 
    NSMutableData *data; 
    NSString *urlString; // key for image cache dictionary 
} 

-(void)loadImageFromURL:(NSURL*)url; 
- (void)StoreImage:(UIImage*)image String:(NSString *)str; 
@end 

,并在.M

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) 
    { 
    } 
    return self; 
} 


- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 


- (void)dealloc 
{ 
    [connection cancel]; 
    [connection release]; 
    [data release]; 
    [super dealloc]; 
} 

-(void)loadImageFromURL:(NSURL*)url 
{ 
    if (connection != nil) 
    { 
     [connection cancel]; 
     [connection release]; 
     connection = nil; 
    } 
    if (data != nil) 
    { 
     [data release]; 
     data = nil; 
    } 

    if (imageCache == nil) // lazily create image cache 
     imageCache = [[ImageCache alloc] initWithMaxSize:2*1024*1024]; // 2 MB Image cache 

    [urlString release]; 
    urlString = [[url absoluteString] copy]; 
    UIImage *cachedImage = [imageCache imageForKey:urlString]; 
    if (cachedImage != nil) { 
     if ([[self subviews] count] > 0) 
     { 
      [[[self subviews] objectAtIndex:0] removeFromSuperview]; 
     } 
     UIImageView *imageView = [[[UIImageView alloc] initWithImage:cachedImage] autorelease]; 
     imageView.contentMode = UIViewContentModeScaleAspectFill; 
     imageView.autoresizingMask = 
      UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     [self addSubview:imageView]; 
     imageView.frame = self.bounds; 
     [imageView setNeedsLayout]; // is this necessary if superview gets setNeedsLayout? 
     [self setNeedsLayout]; 
     return; 
    } 

#define SPINNY_TAG 5555 

    UIActivityIndicatorView *spinny = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    spinny.backgroundColor=[UIColor whiteColor]; 
    // spinny.tag = SPINNY_TAG; 
    // spinny.center = self.center; 
    [spinny startAnimating]; 
    [self addSubview:spinny]; 
    [spinny release]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)incrementalData 
{ 
    if (data==nil) 
    { 
     data = [[NSMutableData alloc] initWithCapacity:2048]; 
    } 
    [data appendData:incrementalData]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection 
{ 
    [connection release]; 
    connection = nil; 

    UIView *spinny = [self viewWithTag:SPINNY_TAG]; 
    [spinny removeFromSuperview]; 
    if ([[self subviews] count] > 0) 
    { 
     [[[self subviews] objectAtIndex:0] removeFromSuperview]; 
    } 
    UIImage *image = [UIImage imageWithData:data]; 
    [imageCache insertImage:image withSize:[data length] forKey:urlString]; 
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease]; 
    imageView.contentMode = UIViewContentModeScaleAspectFill; 
    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [self addSubview:imageView]; 
    imageView.frame = self.bounds; 
    [imageView setNeedsLayout]; 
    [self setNeedsLayout]; 
    [data release]; 
    data = nil; 
} 

而在你的课堂,你只是显示的tableview导入AsyncImageView.h并在tableView中cellForRowAtIndexPath写入

AsyncImageView *asyncImageView = nil; 

    UIImageView *cellImage = (UIImageView *)[cell viewWithTag:1]; 
    asyncImageView = [[AsyncImageView alloc] initWithFrame:cellImage.frame] ; 
    [asyncImageView loadImageFromURL:YourImageURL]; 
    asyncImageView.backgroundColor=[UIColor clearColor]; 
    [cell.contentView addSubview:asyncImageView]; 

我已经使用它,工作正常。希望它也适用于你.. :)

+0

什么是imageCache? – Raju 2013-03-08 10:57:56

0

你懒洋洋地加载图像的tableview。 Here是苹果公司的典范。

+0

我没有得到如何在我的程序中使用惰性图像加载.. – Raju 2013-03-08 11:04:14