如何通过MPMoviePlayerController |添加UIImageView iOS | 目标C

我有一个使用MPMoviePlayerController显示video的控制器。 我需要在video上放置一个图片。

我想用下面的代码,但它不显示。 我错过了什么?

// method to play the video - (void)playVideoInLoopMode:(BOOL)loop { NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"m4v"]]; MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:url]; mp.controlStyle = MPMovieControlStyleNone; if (loop) { mp.repeatMode = MPMovieRepeatModeOne; } mp.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height); self.player = mp; [self.view addSubview:self.player.view]; [self.player prepareToPlay]; [self.player play]; } // method to add the image - (void) addImageLayer { image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage"]]; image.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); [self.view addSubview:image]; } 

首先,我正在用方法运行video:[self playVideoInLoopMode:YES]和5秒钟后,我试图把图像层的方法[self addImageLayer];

在我的AppDelegate.h中,我在didFinishLaunchingWithOptions中有这个代码:

  MyViewController *myVC = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil]; [self.window addSubview:myVC.view]; [self.window makeKeyAndVisible]; 

尝试将图像视图添加到mp.view,然后将mp.view添加到常规视图,例如,如果图像总是相同的,可以将它添加到您的起始代码中,并删除方法以添加视图。 。

 // method to play the video - (void)playVideoInLoopMode:(BOOL)loop { NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"m4v"]]; MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:url]; mp.controlStyle = MPMovieControlStyleNone; if (loop) { mp.repeatMode = MPMovieRepeatModeOne; } mp.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height); self.player = mp; [self.view addSubview:self.player.view]; image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage.png"]]; image.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); [self.player addSubview:image]; [self.player prepareToPlay]; [self.player play]; }