apple sample lazytableimages
1,首先设置横向显示的uitableview
self.customTableview.transform = CGAffineTransformMakeRotation(M_PI/-2);
同时需要将cell也加以旋转否则其内部的图片是反的
cell.contentView.transform = CGAffineTransformMakeRotation(M_PI/2);
2,使用cell的imageview来实现图片的加载
cell.imageView.image = [UIImage imageNamed:@"timeline_image_loading@2x.png"];
这里需要给imageview首先添加占位的图片,否则的话当滚动时后面的cell会因为复用而加载前面已经显示的image
cell.imageView.frame = CGRectMake(0.0f, 0.0f, cell.frame.size.width, self.customTableview.frame.size.height);
3,判断下载图片
if (!self.record.appIcon)
{
{
//if the user does not dragging the scroll view then will start the download
if (self.customTableview.dragging == NO && self.customTableview.decelerating == NO)
{
NSString *startURL = self.record.photoURL;
//start the download if the image url is no tempty
if (startURL && (NSNull*)startURL != [NSNull null])
{
NSString *endURL = [NSURL URLWithString:startURL];
if (endURL)
{
[self startIconDownload:self.record withButtonWidth:self.customTableview.frame.size.width andButtonHeight:self.customTableview.frame.size.height forButtonIndex:indexPath.row andIndexPath:indexPath];
}
}
}
}
}else
{
cell.imageView.image = self.record.appIcon;
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit]; //将裁剪好的图片以指定的大小显示出来
}
- (void)startIconDownload:(PhotosRecord *)appRecord withButtonWidth:(float)width andButtonHeight:(float)height forButtonIndex:(int)tag andIndexPath:(NSIndexPath *)indexPath{
PhotosDetailDownloader*iconDownloader = [self.imageDownloadsInProgress objectForKey:indexPath];;
if (iconDownloader == nil)
{
iconDownloader = [[PhotosDetailDownloader alloc] init];
iconDownloader.kids = appRecord;
[iconDownloader setAlbumPhotosDownloadCompletionHandler:^{
// Display the newly loaded image
if (appRecord.appIcon){
NSLog(@"downloaded index here is %d",indexPath.row);
UITableViewCell *cell = [self.customTableview cellForRowAtIndexPath:indexPath];
cell.imageView.image = appRecord.appIcon;
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
[cell setNeedsLayout]; //这句很重要,否则下载成功后的首个cell和其后已经在屏幕中显示的cell的image无法显示出来。关于这点详见下面的回复
[self.imageDownloadsInProgress removeObjectForKey:indexPath];
}
// Remove the IconDownloader from the in progress list.
}
];
//防止图片重复下载?
if (iconDownloader) {
[self.imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
}
[iconDownloader startDownloadWithWidth:width andHeight:height];
[iconDownloader release];
}
}
When a I fixed the issue by calling
I found the completion block happens in the background so that necessitates performing my UI work on the main thread. Of course this solution won't account for cell reuse and so forth, but at least solves why the cell's image wouldn't appear :) Hope this helps! |
cell.imageView.image = [UIImage imageWithData:data]
works as long as there's been a placeholder image set in the cell within thetableView:cellForRowAtIndexPath:
method, but if you don't provide a placeholder, this same technique doesn't work? Would you mind posting how you're setting up the placeholder image? – Carl Veazey Aug 31 '12 at 5:34cell.imageView.image = [UIImage imageNamed:@"pic0.png"];
– 動靜能量 Aug 31 '12 at 5:44