2011-04-18 40 views
1

我与谷歌的YouTube API的工作,和我使用这个代码在这里http://pastebin.com/vmV2c0HT如何解决的UITableView缓慢滚动 - 使用YouTube API

减慢下来代码中发现这是一个在这里 cell.imageView.image = [UIImage imageWithData:data];

当我删除这些代码是顺利滚动。任何想法如何我可以从谷歌加载图像,但仍然顺利滚动?我读过一些关于以不同的方式加载图像的答案,但我仍然想不通我怎么会让我与正在使用的代码工作。

任何帮助将不胜感激。

贝娄是完整的代码。

#import "RootViewController.h" 

@interface RootViewController (PrivateMethods) 
- (GDataServiceGoogleYouTube *)youTubeService; 
@end 


@implementation RootViewController 

@synthesize feed; 

- (void)viewDidLoad { 
    NSLog(@"loading"); 

    GDataServiceGoogleYouTube *service = [self youTubeService]; 

    NSString *uploadsID = kGDataYouTubeUserFeedIDUploads; 
    NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"BmcCarmen" 
                 userFeedID:uploadsID]; 



    [service fetchFeedWithURL:feedURL 
        delegate:self 
      didFinishSelector:@selector(request:finishedWithFeed:error:)]; 

    [super viewDidLoad];  
} 

- (void)request:(GDataServiceTicket *)ticket 
finishedWithFeed:(GDataFeedBase *)aFeed 
      error:(NSError *)error { 

    self.feed = (GDataFeedYouTubeVideo *)aFeed; 

    [self.tableView reloadData]; 


} 


#pragma mark Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 






// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [[feed entries] count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell. 
    GDataEntryBase *entry = [[feed entries] objectAtIndex:indexPath.row]; 
    NSString *title = [[entry title] stringValue]; 
    NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry mediaGroup] mediaThumbnails]; 

    cell.textLabel.text = title; 

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]]; 
    cell.imageView.image = [UIImage imageWithData:data]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 100.0f; 
} 




- (void)dealloc { 

    [super dealloc]; 
} 


- (GDataServiceGoogleYouTube *)youTubeService { 
    static GDataServiceGoogleYouTube* _service = nil; 

    if (!_service) { 
     _service = [[GDataServiceGoogleYouTube alloc] init]; 


     [_service setShouldCacheDatedData:YES]; 
     [_service setServiceShouldFollowNextLinks:YES]; 
    } 

    // fetch unauthenticated 
    [_service setUserCredentialsWithUsername:nil 
            password:nil]; 

    return _service; 
} 

@end 

回答

3

GCD是一件美妙的事情。这是我做的:



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"youTubeCell"; 
     UITableViewCell *cell = nil; 

     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     // Configure the cell. 
     GDataEntryBase *entry = [[feed entries] objectAtIndex:indexPath.row]; 
     NSString *title = [[entry title] stringValue]; 
     NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry mediaGroup] mediaThumbnails]; 

     cell.textLabel.text = title; 
     // Load the image with an GCD block executed in another thread 
     dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL); 
     dispatch_async(downloadQueue, ^{ 
      NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]]; 
      UIImage * image = [UIImage imageWithData:data]; 

      dispatch_async(dispatch_get_main_queue(), ^{    
       cell.imageView.image = image; 
       cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 
       [cell setNeedsLayout]; 
      }); 
     }); 
     dispatch_release(downloadQueue); 

     return cell; 
    } 

滚动现在非常流畅。唯一的缺点是,当你快速地滚动,细胞被重新使用,有时图像的改变,因为新进来了。

+1

这极大地提高了我的tableview的性能,但是你提到它可以滚动快还是我的时候重新使用数据如果我快速滚动,有时会弹出图像。有没有办法解决这个问题?只是它看起来相当闪烁,否则。 – 2012-12-20 14:50:23