【问题标题】:YouTube videos inside UITableView iPhone AppUITableView iPhone App 内的 YouTube 视频
【发布时间】:2011-07-09 18:40:36
【问题描述】:

我目前有一个表格视图,其中嵌入了自定义单元格内的 youtube 视频。

我这样做是因为根据我的研究,这似乎是让视频在不离开我的应用程序的情况下加载的唯一方法。

问题是这样的:

缩略图需要一段时间才能加载。

当我向下滚动视频列表时,它必须不断加载缩略图。

如果我向上滚动...它会再次尝试加载视频缩略图。

有没有人对更好的方法或让表格单元格保留数据而不是替换它的方法有任何建议?

我的代码如下所示:

CustomVideoCell *cell = (CustomVideoCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomVideoCell"];

if (cell == nil) {

UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"CustomVideoCell" bundle:nil];

cell = (CustomVideoCell *)temporaryController.view;

[temporaryController release];


GDataEntryBase *entry = [[self.feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry title] stringValue];
NSString *videoID = [[(GDataEntryYouTubeVideo *)entry mediaGroup] videoID];


NSString *htmlString = 
[
 [NSString alloc] 
 initWithFormat:@"<html><head><meta name = \"viewport\" content = \"initial-scale = 2.0, user-scalable = no, width = 110\"/></head><body style=\"background:#000;margin-top:0px;margin-left:0px\"><div><object width=\"110\" height=\"90\"><param name=\"movie\" value=\"http://www.youtube.com/v/%@&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/%@&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"110\" height=\"90\"></embed></object></div></body></html>", 
 videoID, videoID
 ];


[[[cell.web subviews] lastObject] setScrollEnabled:NO];
[cell.web loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.website.com"]];

cell.title.text = title;

干杯

【问题讨论】:

  • 有没有人知道任何好的线程和/或教程,因为我想做这样的事情,但不知道从哪里开始。对不起,我是新手。

标签: iphone objective-c ios uitableview youtube


【解决方案1】:

不要出列和重复使用表格单元格。因为您一直在分配新的单元格,所以性能会受到影响,但它应该可以工作。当您收到内存警告时,请开始释放单元格。

【讨论】:

  • 谢谢,我试过了,但是由于每个单元格都包含一个网络视图,当它们离开屏幕时,它们似乎卸载了......然后当它们回到屏幕上时,它们再次加载。 ...这反过来又迫使它从 Youtube 重新加载缩略图
  • 您是否考虑过使用一个大的 UIWebView 来实现表格?
【解决方案2】:

在这些情况下,我所做的是创建一个函数,该函数用我需要的表格单元格填充可变数组。
然后在方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我根据这样的索引返回数组中的单元格:

return [cellArray objectAtIndex:indexPath.row];

我以这种方式创建了非常大的可变数组并且没有内存问题,但我想这取决于您应用程序中的其他内容。

【讨论】:

  • 谢谢伙计,这个方法效果很好,也停止了滚动的滞后!
  • @Zigglzworth 缓存单元需要一些时间。你在哪里实现它? viewDidLoad?
【解决方案3】:

只缓存网页视图,而不是整个表格单元格。这样你仍然可以重复使用这些单元格。 我从这里使用了 YouTubeView http://iosdevelopertips.com/video/display-youtube-videos-without-exiting-your-application.html

 -(void) cacheYouTubeViews {
 NSArray * videos  = [self.fetchedResultsController fetchedObjects]; 
 self.myVideosCache = [[NSMutableArray alloc] initWithCapacity:[videos count]]; 

 for (Video * video in videos) {
    YouTubeView * youTubeView = [[YouTubeView alloc] 
                                 initWithStringAsURL: video.link
                                 frame:CGRectMake(0, 0, 
                                                  kVideoThumbnailSize, 
                                                  kVideoThumbnailSize)];
    [self.myVideosCache addObject:youTubeView]; 


   }


}

然后在你的 cellForRowAtIndexPath 方法中替换单元格的 YouTubeView:

   YouTubeView * youTubeView = [self.myVideosCache objectAtIndex:indexPath.row]; 
   [cell.contentView addSubview:youTubeView];

请务必从单元格的子视图层次结构中删除旧的 YouTubeView。
当表格滚动时,我发现内存泄漏,这可能是在 UITableView 中使用 UIWebView 的一般问题。

【讨论】:

    猜你喜欢
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2011-09-11
    • 2013-03-26
    • 2015-01-07
    • 2011-11-21
    • 2013-06-28
    相关资源
    最近更新 更多