Printing UIView content to UIImage works in simulator but not in device

I am writing an IOS application and I want to create a duplicate of a video player and display this duplicate just below. The user will not be able to interact with this doppelganger, as it simply reflects.

To do this, I have successfully displayed the video (MPMoviePlayerController) in the UIViewer and I am trying to copy the content of the player view attribute in the UIImage just below.

Here is my code:

@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 fine on a simulator, which gives the following output:

http://i.stack.imgur.com/umHsE.png

You can see that I have exactly the expected behavior: a video clone refreshed every 0.03 seconds.

But when I play this on my iPhone I get this:

http://i.stack.imgur.com/6tSZQ.png

As you can see, only the controllers are shown, but there is no video content at all! I don't understand why, since it works well in the simulator ... How would you explain this error? Do you think this is related to Apple's policy of preventing copying of video content? Thanks in advance!

+3


source to share





All Articles