如何获取视频列表的缩略图?
问题描述:
我想创建一个表视图来列出所有的视频信息。我需要从互联网上获取缩略图。如何获取视频列表的缩略图?
我请求加载视图时每个视频的缩略图。但是当请求结束时,通知中心会通知所有的视频对象。所以所有的表格单元将同时更新图像。最后,所有表格单元显示最后完成的相同的缩略图图像。
奇怪的是它在IOS5上正常工作。只有当我在IOS4上运行应用程序时才会发生。
这里是我的代码:
-(void)viewDidLoad{
dataArray = [[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"vedio" ofType:@"plist"]] retain];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:nil];
ctlArray = [[NSMutableArray alloc] init];
for(int i=0; i<[dataArray count]; i++){
MPMoviePlayerController * vedio = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:[[dataArray objectAtIndex:i]objectForKey:@"url"]]];
//[vedio prepareToPlay];
vedio.shouldAutoplay = NO;
vedio.controlStyle = MPMovieControlStyleEmbedded;
NSMutableDictionary * data = [NSDictionary dictionaryWithObjectsAndKeys:vedio,@"controller",[NSNumber numberWithInt:i],@"index", nil];
[ctlArray addObject:data];
NSMutableArray * allThumbnails = [NSMutableArray arrayWithObjects:[NSNumber numberWithDouble:2.0],nil];
[vedio requestThumbnailImagesAtTimes:allThumbnails timeOption:MPMovieTimeOptionExact];
}
......
}
-(void)receiveNotification:(NSNotification *)notify{
id pid = [notify object];
for(int i=0; i<[ctlArray count]; i++){
NSMutableDictionary * o= [NSMutableDictionary dictionaryWithDictionary:[ctlArray objectAtIndex:i]];
if (pid == [o objectForKey:@"controller"]) {
NSDictionary * d = [notify userInfo];
if([d objectForKey:@"MPMoviePlayerThumbnailImageKey"] != nil){
UIImage * recvdata = [d objectForKey:@"MPMoviePlayerThumbnailImageKey"];
[o setObject:recvdata forKey:@"image"];
[ctlArray replaceObjectAtIndex:i withObject:o ];
NSIndexPath *durPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell * cell = [tblV cellForRowAtIndexPath:durPath];
if(cell != nil){
UIImageView* iv = (UIImageView*)[cell viewWithTag:10000];
[iv setImage:recvdata];
}
}
break;
}
}
}
任何sugesstion将appreciate.Thanks!
答
通常,您不会直接获取单元格并更新它们。推荐的方法是更新您的基础模型(数据源)。然后,调用reloadRowsAtIndexPaths:withRowAnimation:
方法,传递索引路径。
然后应该用新值重绘单元格。
另外请注意,模拟器,顾名思义,不是一个模拟器,事实上,它是运行在一个物理设备不同的库。偏差可能发生,并且在调试时应该非常小心。