gpt4 book ai didi

ios - 从 YouTube JSON 数据 Objective C 获取缩略图

转载 作者:行者123 更新时间:2023-11-29 02:30:48 24 4
gpt4 key购买 nike

我想在自定义单元格中显示 YouTube 视频的缩略图。我有 JSON 链接,但不确定如何获取实际图像?我的代码在下面!

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

*)indexPath
{
static NSString *CellIdentifier = @"RemedyYouTubeTableViewCell";
RemedyYouTubeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RemedyYouTubeTableViewCell" owner:nil options:nil];
for(id obj in topLevelObjects) {
if([obj isKindOfClass:[UITableViewCell class]]) {
cell = obj;
break;
}
}


}

// Configure the cell...

// cell.videoThumbnail.image = [UIImage imageNamed:[thumbNailArray objectAtIndex:indexPath.row]];

NSArray *subArray = [subDict objectForKey:@"items"];
if ([subArray count]) {
for (NSDictionary *dictItem in subArray) {
cell.titleLabel.text = [dictItem objectForKey:@"title"];

NSNumber *totalSeconds = [dictItem objectForKey:@"duration"];
int *minutes = totalSeconds.intValue/60;
int *seconds = totalSeconds.intValue%60;
NSString *finalDuration =[NSString stringWithFormat:@"%im %is", minutes,seconds];
cell.durationLabel.text = finalDuration;


NSString *separatorString = @"T";
NSString *firstUploadString = [dictItem objectForKey:@"uploaded"];
NSString *uploadString = [firstUploadString componentsSeparatedByString:separatorString].firstObject;
cell.dateUploadLabel.text = uploadString;

NSString *viewCount = [NSString stringWithFormat:@"%@",[dictItem objectForKey:@"viewCount"]];
cell.viewsLabel.text = viewCount;

NSString *thumbnailURL = [dictItem objectForKey:@"thumbnail"];

}
}



return cell;
}

其他一切都很好,但我该如何处理缩略图 URL?谢谢!

我的代码现在看起来像这样......

NSString *viewCount = [NSString stringWithFormat:@"%@",[dictItem objectForKey:@"viewCount"]];
cell.viewsLabel.text = viewCount;

NSDictionary *thumbnailDict = [dictItem objectForKey:@"thumbnail"];
//NSString *thumbnailURl = [thumbnailDict objectForKey:@"sqDefault"];
//UIImage *thumbNailImage = nil;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSString *thumbnailURL = [thumbnailDict objectForKey:@"sqDefault"];
NSLog(thumbnailURL);
NSURL *url = [NSURL URLWithString:thumbnailURL];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *thumbNailImage = [UIImage imageWithData:imageData];

dispatch_async(dispatch_get_main_queue(), ^{
// Here in main thread set image to your cell imageView.
// e.g.
[cell.videoThumbnail setImage:thumbNailImage];
});
});





};
}



return cell;
}

最佳答案

您只需要从该 url 获取 NSData。确保 url 有效且可用。

@try {

UIImage *thumbNailImage = nil;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSString *thumbnailURL = [dictItem objectForKey:@"thumbnail"];
NSURL *url = [NSURL URLWithString:thumbnailURL];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *thumbNailImage = [UIImage imageWithData:imageData];

dispatch_async(dispatch_get_main_queue(), ^{
// Here in main thread set image to your cell imageView.
// e.g.
[cell.imageView setImage:thumbNailImage];
});
});

}
@catch (NSException *exception) {
NSLog(@"Exception :%@",exception.debugDescription);
}

关于ios - 从 YouTube JSON 数据 Objective C 获取缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26907679/

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