开发者

YouTube videos inside UITableView iPhone App

I currently have a tableview with youtube videos embedded inside of the custom cells.

I did this because from my research it seemed like the only way to allow the videos to load without leaving my application.

The problem is this:

The thumbnai开发者_如何学Gols take a while to load.

As I scroll down the list of videos, it keeps having to load the thunbnails.

If I scroll back up...it tries to load the video thumbnails yet again.

Has anyone got any suggestions on either better ways of doing this, or ways of getting the table cells to keep the data and not replace it?

My code looks like this:

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;

Cheers


What I do in these cases is creating a function which fills a mutable array with the table cells I need.
Then in the method:

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

I return the cell in the array based on the index like this:

return [cellArray objectAtIndex:indexPath.row];

I have created very large mutable arrays this way and had no memory issues but I guess it depends on other things in your app.


Cache just the web views, not the entire table cell. That way you can still reuse the cells. I used YouTubeView from here 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]; 


   }


}

Then in your cellForRowAtIndexPath method replace the cell's YouTubeView:

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

Be sure to remove the old YouTubeView from the cell's subview hierarchy.
I am finding memory leaks when the table scrolls, possibly a general problem with using UIWebView within UITableView.


Don't dequeue and reuse the table cells. There will be a performance hit because you're allocating new cells all the time, but it should work. When you get a memory warning then start releasing the cells.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜