2012-07-17 74 views
1

我試圖在懶惰模式下加載我的uitableviewcells的圖像。在桌面視圖中加載圖像的延遲

我試圖以最簡單的方式做到這一點,我看到很多例子,但他們比我的目標更進一步。

這是林目前做的,它不工作:

// Configure the cell... 
    Info *info = [self.Array objectAtIndex:indexPath.row]; 
    cell.textLabel.text = info.name; 
    cell.detailTextLabel.text = info.platform; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 


    //The image is downloaded in asynchronous 

    NSBlockOperation *downloadingImgBlock = [NSBlockOperation blockOperationWithBlock:^{ 
     NSString* imageURL = info.imgURL; 
     NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:imageURL]]; 
     cell.imageView.image = [UIImage imageWithData:imageData]; 
    }]; 
[self.queue addOperation:downloadingImgBlock]; 

爲什麼它不工作?它是如何工作的?

+0

難道你嘗試在主線程上設置圖像,調用dispatch_async並傳遞主線程 – rckoenes 2012-07-17 13:02:23

+0

你有沒有試過Grand Dispatch呢? – holex 2012-07-17 13:09:00

+0

對這兩個問題都沒有,你能告訴我怎麼做嗎? – 2012-07-17 13:10:35

回答

0

我終於成功地做到這一點設置圖像用:

– performSelectorOnMainThread:withObject:waitUntilDone: 

圖像被下載後

5

男人,AsyncImageView是你的朋友!只需設置圖像的URL,一切都爲您處理,下載,緩存。這很棒。

+0

我不能使用庫,它必須以最簡單的方式在我的代碼中完成,但是謝謝 – 2012-07-17 13:01:17

+0

這不是一個庫。您可以將其源代碼添加到您的代碼中。 – 2012-07-17 13:02:53

+0

我無法使用第三方零件 – 2012-07-17 13:03:14

1

試試下面的代碼

...... 
    __block UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    __block UIImage *img; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imageURL]]]; 

     dispatch_async(dispatch_get_main_queue(),^{ 

      cell.imageView.image = img; 

      } 
     }); 


    }); 
    ...... 
0

試試這個,我一直使用該方案異步加載圖片:

(我還沒有找到最簡單的方法。)

- (void)inAnyMethod { 

    // ... 

    void (^blockLoadPhoto)() = ^{ 
     UIImage *_imageTemporary = [UIImage imageNamed:@"..."]; 
     if (_imageTemporary) { 
      [self performSelectorOnMainThread:@selector(setPhoto:) withObject:_imageTemporary waitUntilDone:true]; 
     } 
    }; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), blockLoadPhoto); 

    // ... 

} 

- (void)setPhoto:(UIImage *)photo { 
    // do something with the loaded image 
} 
1

使用第三方庫源代碼將是最簡單的,但這裏是你如何做到這一點在代碼中。你會想做出NSMutableArray無論是作爲一個屬性在.h或在您.m像這樣的頂部:

@implementation viewController{ 
    NSMutableArray *picList; 
} 

-initWithCoder:,或者任何初始化你超載:

picList = [[NSMutableArray alloc] init]; 

– tableView:cellForRowAtIndexPath:(fullPicURL是URL):

if([self imageExists:fullPicURL]){ 

    shoePic = [picList objectForKey:fullSmallPicURL]; 

} else { 

    NSURL *picURL = [NSURL URLWithString:fullPicURL]; 

    shoePic = [UIImage imageWithData:[NSData dataWithContentsOfURL:picURL]]; 
    NSDictionary *thisImage = [NSDictionary dictionaryWithObject:shoePic forKey:fullSmallPicURL]; 
    [cachedImages addEntriesFromDictionary:thisImage]; 
} 

其中-imageExists:是FUNC您編寫的內容會檢查字典中的url。對象是圖片本身,關鍵是url。然後你做cell.image = showPic;,或者任何你想調用它的東西,然後你就完成了緩存。爲了異步加載它們,這樣做:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    NSData *data = [NSData dataWithContentsOfURL:shoes]; 
    [self performSelectorOnMainThread:@selector(fetchedShoeData:) withObject:data waitUntilDone:YES]; 

}); 

,並在fetchedData結束:

[self.tableView reloadData]; 

則只需確保您編輯–numberOfSectionsInTableView:來改變自己是否具有。可能是你需要做的就是它的工作100%,一些其他的事情,讓我知道