所以我正在開發包含有在細胞圖像表格視圖的應用程序。我從URL下載所有這些圖像並將它們存儲在一個數組中。我需要異步加載這些圖像,所以我決定使用NSURLConnectionDataDelegate。我試過這個代碼:獲取圖像異步
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[URLsArray objectAtIndex:row]]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receicedData = [[NSMutableData alloc] init];
} else {
NSLog(@"Failed.");
}
return cell;
}
但它不起作用。並且我不知道該寫什麼:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
將圖像設置爲單元格中的圖像視圖。誰能幫忙?
但是,如果使用setImageWithURL,它會同步加載圖片,不是嗎?如果是,如何使它異步? – Noobass
如果按照文檔的鏈接,你會看到,它明確指出,圖像將異步下載。 – javieralog
好的,但有什麼辦法沒有使用任何額外的庫? – Noobass