我有一个 Controller ,可以使用 MPMoviePlayerController 显示视频。我需要在视频上放置图像。
我正在尝试使用以下代码,但它没有显示。我缺少什么?
// 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];
}
首先,我使用以下方法运行视频:[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];
最佳答案
尝试将 ImageView 添加到 mp.view,然后将 mp.view 添加到常规 View 例如,如果图像始终相同,您可以将其添加到起始代码中并删除添加 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];
}
关于ios - 如何在 MPMoviePlayerController 上添加 UIImageView | iOS | objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11113375/