5
我通過以下代碼將youtube視頻縮略圖嵌入到我的UITableView單元中。但是,每當我滾動表格時,視頻縮略圖將在單元格離開屏幕並返回時重新加載。在UITableViewCell中嵌入YouTube視頻:如何緩存以防止重新加載?
什麼是緩存縮略圖的最有效方式,以便它們只能第一次加載?
- (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;
}
你有沒有找到你的問題或任何解決方法的答案 – Alix