Tôi đang nhúng hình thu nhỏ video trên YouTube vào Ô UITableView của mình thông qua mã sau đây. Tuy nhiên, bất cứ khi nào tôi cuộn bảng, hình thu nhỏ video sẽ tải lại khi ô rời khỏi màn hình và trả về.Video được nhúng trên YouTube trong UITableViewCell: Cách cache để ngăn tải lại?
Cách hiệu quả nhất để lưu vào bộ nhớ cache hình thu nhỏ để chúng chỉ tải lần đầu tiên?
- (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;
}
Bạn đã tìm ra câu trả lời cho câu hỏi của mình hay cách giải quyết khác – Alix