iPhone - 下载异步工作,但不加载异步

iPhone - 下载异步工作,但不加载异步

问题描述:

我正在为表格视图构建自定义单元格.我正在尝试从互联网加载图像,为此,我正在使用异步下载.正在下载图像,但它没有在我的单元格中显示此图像.我已经尝试在正常视图中显示并且工作正常.如果图像已经下载或者我滚动表格视图并再次显示单元格,它也可以工作.有人知道这是怎么回事吗?

I'm building my custom cell for a table view. I'm trying to load an image from internet and for it, i'm using async download. The image is being downloaded, but it's not showing this image in my cell. I already tried to show in a normal view and it's working fine. It does work too if the image is already downloaded or if I roll the table view and show the cell again. Does anybody knows what's going on?

代码:

下载ImageManager.m

DownloadImageManager.m

-(id)initWithImageName:(NSString *)imageAddress{
    self = [super initWithFrame:CGRectMake(10, 5, 100, 100)];
    if (self){
        self.urlString = imageAddress;
        av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
        av.frame = self.frame;
        [av setBackgroundColor:[UIColor greenColor]];
        [self addSubview:av];
        [av startAnimating];
        [self checkImage];
    }
    return self;
}

-(void)checkImage{
    bool isImageOnSysten = [self isImageOnFileSystem];
    if (isImageOnSysten) {
        //If image is on the system, loads the image, it's working fine here
        NSLog(@"CSantos: isImageOnSysten %@ is on system", self.urlString);
    } else {
        //here is the problem:
        [self downloadImage];
    }
}

-(void)downloadImage{
    NSURL *url = [NSURL URLWithString:self.urlString];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request setAllowCompressedResponse:YES];
    [request setQueuePriority:NSOperationQueuePriorityLow];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request setTimeOutSeconds:25];
    [request setNumberOfTimesToRetryOnTimeout:3];

    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSData *responseData = [request responseData]; 
    NSArray *words = [self.urlString componentsSeparatedByString:@"/"];
    NSString *fileName = [words lastObject];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
    NSError *error = nil;
    [responseData writeToFile:writablePath options:NSDataWritingAtomic error:&error];
    NSLog(@"Write returned error: %@", [error localizedDescription]);
    [av stopAnimating];
    [av removeFromSuperview];
}

CellForProgram.m

CellForProgram.m

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

        textLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 31, 235, 40)] ;
        [self.contentView addSubview:textLabel];

        photo = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 70, 70)];
        [photo setBackgroundColor:[UIColor blueColor]];
        photo.image = imagePhoto.image;
        [self.contentView addSubview:photo];
    }
    return self

手机来电

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    CellForProgram *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] ;
    if (cell == nil) {
        cell = [[[CellForProgram alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [speaker objectAtIndex:indexPath.row];

    DownloadImageManager *imageManager = [[DownloadImageManager alloc] initWithImageName:[images objectAtIndex:indexPath.row]];
    [cell.photo setImage:imageManager.image];
    return cell;
}

我说过我不需要在 DownloadImageManager 上做这种改变!但是感谢您的帮助,它帮助我解决了其他问题!

I told I wouldn't need to do this kind of change on DownloadImageManager! But thanks for trying to help, it helped me in other stuff I was stucked!

CellForProgram.m

CellForProgram.m

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

        textLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 31, 235, 40)] ;
        [self.contentView addSubview:textLabel];

        imagePhoto = [[DownloadImageManager alloc] initWithImageName:imageAdress.text];
        [self.contentView addSubview:imagePhoto];
    }
    return self
}

DownLoadImageManager.m:添加这个方法

DownLoadImageManager.m: add this method

-(void)changeImage:(NSString *)newImage{
    self.urlString = newImage;
    [self checkImage];
}

手机来电

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    CellForProgram *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] ;
    if (cell == nil) {
        cell = [[[CellForProgram alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [speaker objectAtIndex:indexPath.row];

    [cell.imagePhoto changeImage:[images objectAtIndex:indexPath.row]];
    return cell;
}