2015-12-11 51 views
0

我有在我的在我的tableview細胞閃爍圖像每次表reloadData方法被調用的問題。發生閃爍是因爲每次重新加載表時都會下載圖像。我如何製作它,以便每次只下載一次該圖像,而不是一次?圖像閃爍每次表被重新加載(iOS的,客觀-C)

這是SelectStationViewController.m中的cellForRowAtIndexPath代碼。這個類處理tableView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    StationRest * StationRest = [[CurrentUser sharedInstance].userStations objectAtIndex:indexPath.row]; 
    StationListCell *cell= [[StationListCell alloc]initWithFrame:CGRectMake(0, 0, 375,88)]; 
    cell.cellDelegate = self; 

    //This method below downloads the image into the cell. 
    [cell configureCellWithStationRest:StationRest forCellType:StationListCellTypeSelect]; 

    return cell; 
} 

這是StationListCell.m中的代碼,它是與單元連接的類。這裏是使用AFNetworking下載圖片的地方。我可以用[[NSData alloc] initWithContentsOfURL方法代替AFNetworking來使用GCD,但我仍然可以得到相同的結果。

-(void)configureCellWithStationRest:(StationRest *)stationRest forCellType:(StationListCellType) cellType{ 

    NSURL *url = [NSURL URLWithString:stationRest.thumbURL]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    requestOperation.responseSerializer = [AFImageResponseSerializer serializer]; 

    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     [self.thumbButton setImage:responseObject forState:UIControlStateNormal]; 
     [self.thumbButton setContentMode:UIViewContentModeScaleAspectFill]; 
     } 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      DLog(@"Image error: %@", error); 
     }]; 

    [requestOperation start]; 

} 

回答

2

可以使用的UIImageView + AFNetworking.h category.It提供方法來從URL和緩存下載圖像it.Please檢查其文檔的詳細信息。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]]; 
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"]; 
[yourImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
    yourImageView.image = image; 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
    NSLog(@"Failed to load image"); 
}]; 
+0

這沒有工作。我正在使用一個按鈕,而不是一個imageview。 –

+0

沒關係。我做了透明按鈕,並把它放在這..這工作。謝謝 –

+0

歡迎@ JoshO'Connor :) –