加载前重置异步UIImageView

时间:2011-08-04 12:21:37

标签: iphone objective-c uiviewcontroller uiimageview uitableview

我的应用程序层次结构如下:

UINavigationController
|_____
      UITableViewController (display products summary with thumbnail image)
      |_____
            UIViewController (display product details with image)

我正在使用以下类在UITableViewController和UIViewController中异步加载图像,因为加载视图太慢,因为它从互联网上获取图像

http://www.markj.net/iphone-asynchronous-table-image

我在UITableViewController中没有任何问题,问题是当单击单元格时它会在UIViewController中加载图像,如果我回到UITableViewController然后单击另一个单元格,UIViewController会打开前一个图像,直到新图像加载

如何在UIViewController中重置UIImageView,直到加载新图像?

这是加载详细信息视图的代码:

CGRect frame;
frame.size.width=240; frame.size.height=130;
frame.origin.x=0; frame.origin.y=0;
AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
asyncImage.tag = 999;
NSURL *url = [NSURL URLWithString:@"http://i53.tinypic.com/5ezwc4.jpg"];
[asyncImage loadImageFromURL:url];
[detailsViewController.imgProduct addSubview:asyncImage];

请帮忙。

2 个答案:

答案 0 :(得分:1)

这是在asyncImageview方法的loadimagefromurl方法中的bz,它缓存了你的prev数据,你需要一个重置缓存 - 来设置它的

    imageCache = nil; in the method 

但它会影响使用asynimageview的其他视图控制器,

我希望你在你的详细信息视图控制器中有单个图像

更好我应该建议使用GCD

NSString *finalStr = [NSString stringWithFormat:@"/no-image-available.png"];    

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{


    NSURL* url = [NSURL URLWithString:finalStr];

    NSData *imgData = [NSData dataWithContentsOfURL:url];

    dispatch_sync(dispatch_get_main_queue(), ^{



    UIImage *img01 = [UIImage imageWithData:imgData];


    imgView.image = img01;  
    });

});

http://blog.slaunchaman.com/2011/02/28/cocoa-touch-tutorial-using-grand-central-dispatch-for-asynchronous-table-view-cells/

这会给你相同的表现

答案 1 :(得分:0)

值得增强AsyncImageView类。添加您自己的init方法,如下所示:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.image = [UIImage imageNamed:@"loading.png"];
    }
    return self;
}

文件loading.png必须在您的包中,并包含适当的内容,以便在加载远程图像时显示。