ios - 如何停止在表格 View 中重复加载图像?
<p><p>这是我显示来自服务器的图像的代码。加载图像后,当您向下滚动然后返回时,图像会消失,并且在再次加载之前还有另一个延迟。</p>
<pre><code>dispatch_async(kBgQueue, ^{
NSString *profileImageStr = [ objectForKey:@"image_url"];
NSURL* profileImgURL = ;
NSData *profileImgData = ;
NSString *postPhotoStr = [ objectForKey:@"image_url"];
NSURL *postImgURL = ;
NSData *postImageData = ;
dispatch_async(dispatch_get_main_queue(), ^{
if (postImageData && profileImgData != nil) {
cell.thumbnailImg.image = ;
cell.bgImgView.image = ;
}else
{
cell.thumbnailImg.image = ;
cell.bgImgView.image = ;
}
});
}
);
return cell;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我认为您需要为图像实现缓存。将图像存储在 <code>NSCache</code> 中。 </p>
<pre><code>//instance
NSCache *imageCache;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier=nil;
if(>0)
{
simpleTableIdentifier=];
}
else
simpleTableIdentifier = @"ContactTableCell";
ContactTableCell *cell = (ContactTableCell *);
if (cell == nil)
{
NSArray *nib = [ loadNibNamed:@"ContactTableCell" owner:self options:nil];
cell = ;
NSString *photoString=];
UIImage *image = ;
if (image)
{
cell.ProfilePicture.image=image;
}
else
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = ];
UIImage *image = [ initWithData:];
if(image)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *small = ;
cell.ProfilePicture.image=small;
});
;
}
else
cell.ProfilePicture.image=;
});
}
}
return cell;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何停止在表格 View 中重复加载图像?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19216721/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19216721/
</a>
</p>
页:
[1]