gpt4 book ai didi

xcode - 如何解决UITableView中的缓慢滚动-使用YouTube API

转载 作者:行者123 更新时间:2023-12-03 05:28:24 25 4
gpt4 key购买 nike

我正在使用Google YouTube API,并且正在使用此处找到的代码http://pastebin.com/vmV2c0HT

放慢速度的代码就是这里的代码cell.imageView.image = [UIImage imageWithData:data];
当我删除该代码时,它会平滑滚动。关于如何使它从Google加载图像但仍能平滑滚动的任何想法?我已经读过一些关于以不同方式加载图像的答案,但是我仍然不知道如何使用我正在使用的代码来实现该功能。

任何帮助将不胜感激。

波纹管是完整的代码。

   #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

最佳答案

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;
}


滚动现在 super 顺畅。唯一的缺点是,当您快速滚动时,单元会被重复使用,有时图像会随着新图像的进入而改变。

关于xcode - 如何解决UITableView中的缓慢滚动-使用YouTube API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5699257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com