在UIImage中打印UIView的内容在模拟器中工作,但不在设备

时间:2015-07-21 04:05:06

标签: ios uiview uiimageview mpmovieplayercontroller mpmoviewcontroller

我一直在编写IOS应用程序,我希望创建一个视频播放器的精确副本,并在下面显示这个双重。 用户将无法与该双重进行交互,因为它只是反射。

为此,我在UIViewer中成功显示了一个视频(MPMoviePlayerController),并尝试在下面的UIImage中复制播放器的view属性的内容。

到目前为止,这是我的代码:

@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

如您所见,仅显示控制器,但视频的内容完全丢失!我无法理解为什么因为它在模拟器中运行良好...你怎么解释这个bug?您是否认为这与Apple政策有关,可以作为复制视频内容的预防措施?提前谢谢!

0 个答案:

没有答案