gpt4 book ai didi

ios - 当我使用 AVPlayerLayer 时放大镜以错误的方式出现

转载 作者:行者123 更新时间:2023-11-29 12:19:57 24 4
gpt4 key购买 nike

我正在使用 AVPlayerLayer 在我的 UIViewController 实例的后台播放视频。当我在 iPhone 上启动我的应用程序时,我注意到放大镜的显示方式有误。视频规范:H.264、AAC、640x360、MOV。

enter image description here

没有我的视频子图层的放大镜绘图内的背景。只有 subview (按钮、文本字段等)在我的放大镜内绘制。

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];

NSURL *videoURL = [[NSBundle mainBundle] URLForResource:@"bf4" withExtension:@"mov"];
AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

UIView *container = [[UIView alloc] initWithFrame:self.view.bounds];
container.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:container];

AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
[container.layer addSublayer:layer];
layer.frame = container.bounds;
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;

[self.view bringSubviewToFront:self.textField];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[player play];
});
}

有谁知道如何防止这种情况发生?

最佳答案

我想到的唯一解决方案是:

  1. UIImageView放在container View 下:

    self.containerThumbnail = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.containerThumbnail.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.containerThumbnail.backgroundColor = [UIColor clearColor];
    [self.view insertSubview:self.containerThumbnail belowSubview:container];
  2. 成为媒体播放器的时间观察者:

     CMTime bgInterval = CMTimeMake(1, 10);
    __weak __typeof(self)weakSelf = self;
    [player addPeriodicTimeObserverForInterval:bgInterval
    queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
    usingBlock:^(CMTime time)
    {
    [weakSelf locateThumbnail:time];
    }];
  3. 将快照放入您的 UIImageView:

    - (void)locateThumbnail:(CMTime)time
    {
    __weak __typeof(self)weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
    CGImageRef imageRef = [weakSelf.imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    __strong __typeof(weakSelf)strongSelf = weakSelf;
    dispatch_async(dispatch_get_main_queue(), ^(void) {
    strongSelf.containerThumbnail.image = image;
    });
    });
    }

imageGenerator 属性是 AVAssetImageGenerator 类型:

imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;

注意快照在后台更新,因此帧滞后于视频。但这聊胜于无。

关于ios - 当我使用 AVPlayerLayer 时放大镜以错误的方式出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30945425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com