【发布时间】:2011-11-10 12:56:27
【问题描述】:
我通过以下代码将 youtube 视频缩略图嵌入到我的 UITableView 单元中。但是,每当我滚动表格时,当单元格离开屏幕并返回时,视频缩略图就会重新加载。
缓存缩略图以使其仅在第一次加载时最有效的方法是什么?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = @"PlaceholderCell";
SearchResult *searchResult = [self.searchResultArray objectAtIndex:indexPath.row];
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier]autorelease];
UIWebView *videoView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 104, 104)];
videoView.tag = 1;
cell.imageView.image = [UIImage imageNamed:@"Placeholder"];
[cell.contentView addSubview:videoView];
[videoView release];
}
UIWebView *thisVideoView = (UIWebView *)[cell.contentView viewWithTag:1];
NSString *videoHTML = [self embedYouTube:searchResult.url frame:CGRectMake(0, 0, 104, 104)];
[thisVideoView loadHTMLString:videoHTML baseURL:nil];
return cell;
}
- (NSString*)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
return html;
}
【问题讨论】:
-
您找到问题的答案或任何解决方法了吗
标签: objective-c ios uitableview youtube