I've been writing an IOS application, and I wish to create an exact copy of a video player, and display this double just below. The user would not be able to interact with that double since it's just a reflection.
For that, I successfully displayed a video (MPMoviePlayerController) in a UIViewer, and I try to copy the content of the view attribute of the player in a UIImage just below.
Here is my code so far :
@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
This code works perfectly on the simulator, which gives this result :
https://i.stack.imgur.com/umHsE.png
You can see that i have exactly the behaviour expected : a clone of the video, that is refreshed every 0.03 second.
But when I play this on my iPhone device, i get this :
https://i.stack.imgur.com/6tSZQ.png
As you can see, only the controllers are displayed, but the video's content is completely missing ! I can not understand why since it's working well in the simulator... How would you explain this bug? Do you think it's got to do with Apple policy as a prevention to copy video content? Thanks in advance !