我正在使用 iOS 中的视频和图像缓存。我已经实现了图像缓存。现在在同一个缓存中,我想存储视频,我还有以下代码用于将图像和视频存储到缓存中:
-(void)cacheFromURL:(Food*)foodForUrl
{
AppDelegate *app=[[UIApplication sharedApplication]delegate];
[app.imageCache setCountLimit:50];
NSURL *imageUrl=[NSURL URLWithString:foodForUrl.image];
NSURL *videoUrl=[NSURL URLWithString:foodForUrl.video];
UIImage* newImage = [cache objectForKey:imageUrl.description];
if( !newImage )
{
NSError *err = nil;
if(imageUrl!=Nil)
{
newImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl options:0 error:&err]];
}
if(videoUrl!=Nil)
{
moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:videoUrl];
[app.imageCache setObject:moviePlayer forKey:videoUrl.description];
NSLog(@"video caching....%@",videoUrl.description);
}
if( newImage )
{
[app.imageCache setObject:newImage forKey:imageUrl.description];
NSLog(@"caching....");
}
else
{
}
}
}
我有以下用于从缓存中检索的代码。它适用于图像,但不适用于视频:
- (IBAction)playButton:(id)sender
{
AppDelegate *app=[[UIApplication sharedApplication]delegate];
NSURL *url=[[NSURL alloc]initWithString:food.video];
NSLog(@"cached for:%@",url.description);
moviePlayerController=[[MPMoviePlayerController alloc]init];
// moviePlayerController=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:food.video]];
moviePlayerController=[app.imageCache objectForKey:url.description];
moviePlayerController.scalingMode=MPMovieScalingModeAspectFill;
moviePlayerController.view.frame=CGRectMake(320,200, 320,200);
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
}
在这种情况下播放视频有什么问题?任何答案和建议都将是有价值的