【问题标题】:Async image downloader异步图像下载器
【发布时间】:2010-10-28 00:08:55
【问题描述】:

我编写了一个小类来使用 NSURLConnection 来执行图像的下载。 将下载委托给此类以避免阻塞执行的想法。

所以我将目标 UIImageView(通过 ref)和 url 传递给函数并开始下载:

-(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 timeoutInterval: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];
}

完成后设置图像并更新 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];
    }
}

这个系统工作得很好,但我无法更新 UITableViewCell 单元格内的 UIImageView,知道吗? (实际上是当我点击它时更新的单元格) 有更好的方法来实现这个功能吗? (其实需要的是:非阻塞、缓存系统)

【问题讨论】:

    标签: iphone image user-interface asynchronous download


    【解决方案1】:

    塞萨尔

    对于异步图像加载、缓存和线程操作,我使用来自http://github.com/rs/SDWebImage 的 SDImageCache 类。我自己也写了好几次,但这个很棒,我把所有旧代码都扔掉了。

    来自其文档:

    只需#import UIImageView+WebCache.h 标头,然后从tableView:cellForRowAtIndexPath: UITableViewDataSource 方法调用setImageWithURL:placeholderImage: 方法。从异步下载到缓存管理,一切都会为您处理。

    希尔顿

    【讨论】:

    • 它工作得很好,但我在 UITableCellView 上仍然有同样的问题
    【解决方案2】:

    只需按照以下步骤操作:

        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"]];
    

    就是……你已经完成了!!!

    【讨论】:

    • 尝试使用 SDWebImage 但运行时出现 8 个错误。 "compat"-version 调出 10. 有什么提示吗?
    • 是的,我愿意。需要的项目 MapKit.framework 和 ImageIO.framework 添加到 Build Phases 中的“Link Binary With Libraries”中。
    【解决方案3】:
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
        [img_data setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.str_url]]]];
        [pool release];
    

    【讨论】:

      【解决方案4】:

      这个系统工作得很好,但我无法更新 UIImageView 在 UITableViewCell 的单元格内,有什么想法吗? (实际上是细胞 当我点击它时它会更新)有更好的方法来实现 这个功能? (其实需要的是:非阻塞、缓存 系统

      您是否尝试过使用 [tableView reloadData]; 在 connectionDidFinishLoading 方法结束时调用它

      【讨论】:

        【解决方案5】:

        简单地使用SDWebImage。我也在用那个。顺利加载图像。我正在使用下面的代码显示来自 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"];
        }];
        

        成功 部分将显示来自 webService 的图像。而且,如果有任何图像无法加载或任何其他失败问题,您可以简单地在那里加载您自己的图像。 There 是该示例的更多功能。你可以参考一下。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多