我一直在编写一个 IOS 应用程序,我希望创建一个视频播放器的精确副本,并在下面显示这个副本。用户将无法与该替身进行交互,因为它只是一种反射。
为此,我在 UIViewer 中成功显示了一个视频(MPMoviePlayerController),并尝试将播放器的视图属性的内容复制到下面的 UIImage 中。
到目前为止,这是我的代码:
@interface ViewController ()
@property (strong, nonatomic) MPMoviePlayerController *player;
@property (strong, nonatomic) UIImageView *imageView;
- (UIImage *)screenCapture:(UIView *)view;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.xx.xx/serenity.mp4"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.player = player;
player.view.frame = CGRectMake(0, 0, 320, 200);
[self.view addSubview:player.view];
[self.view bringSubviewToFront:player.view];
[player prepareToPlay];
CGRect rect = CGRectMake(0, 310, 320, 200);
_imageView = [[UIImageView alloc]initWithFrame:rect];
[self.view addSubview:_imageView];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(repeateMethod:) userInfo:nil repeats:YES];
// repeateMethod calls the snapshot every 0.03 second
}
- (void)repeateMethod:(NSTimer *)timer
{
_imageView.image = [self screenCapture:_player.view];
}
- (UIImage *)screenCapture:(UIView *)view {
UIImage *capture;
[view snapshotViewAfterScreenUpdates: NO];
UIGraphicsBeginImageContextWithOptions(view.frame.size , NO , 2.0 );
if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[view drawViewHierarchyInRect:view.frame afterScreenUpdates:NO];
} else {
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
}
capture = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return capture;
}
@end
此代码在模拟器上完美运行,结果如下:
http://i.stack.imgur.com/umHsE.png
您可以看到我的行为完全符合预期:视频的克隆,每 0.03 秒刷新一次。
但是当我在我的 iPhone 设备上玩这个时,我得到了这个:
http://i.stack.imgur.com/6tSZQ.png
如您所见,仅显示控制器,但视频内容完全丢失!我不明白为什么,因为它在模拟器中运行良好......你会如何解释这个错误?您是否认为这与 Apple 政策有关,以防止复制视频内容?提前致谢 !