如何缩放CALayer以适应视频图层

时间:2016-05-31 15:50:10

标签: ios objective-c iphone swift uiview

我有CALayer的问题。关键是我想在电影上保存很少的UIViews。基本上我想通过CALayer来做。一切都还可以,app导出带有图层的电影,但带有视图的图层小于视频帧。

我尝试手动缩放图层但完全没有在屏幕上看起来像。如果我在电影上移动UIView更高,那就更高了。

视频具有FullHd框架,UIView的图层取决于用户如何将其放置在屏幕上。

问题是: - 如何渲染具有更好视网膜质量的UIView上下文?

  • 如何缩放图层以完美贴合视频。

有代码:

VideoSaver.saveVideo(videoAsset!) { (composition, size) in
        ////////////////
        //Parent Layer//
        ////////////////

        let parentLayer = CALayer()
        parentLayer.frame = CGRectMake(0, 0, size.width, size.height)

        /////////
        //Sizes//
        /////////
        let xScaleValue = CGRectGetWidth(parentLayer.frame)/CGRectGetWidth(self.view.frame)
        let yScaleValue = CGRectGetHeight(parentLayer.frame)/CGRectGetHeight(self.view.frame)

        ///////////////
        //Video Layer//
        ///////////////
        let videoLayer = CALayer()
        videoLayer.frame = CGRectMake(0, 0, size.width, size.height)

        //////////////////
        //Captions Layer//
        //////////////////

        //Creating UIImage from Captions
        UIGraphicsBeginImageContext(CGSizeMake(self.captionsTextView.bounds.size.width, self.captionsTextView.bounds.size.height+20))
        let currentContext = UIGraphicsGetCurrentContext()

        CGContextTranslateCTM(currentContext, 0, 16)

        self.captionsTextView.layer.renderInContext(currentContext!)
        let captionTextViewImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        let captionsOverlayLayer = CALayer()
        captionsOverlayLayer.contents = captionTextViewImage.CGImage
        captionsOverlayLayer.frame = CGRectMake(self.captionsTextView.frame.origin.x * xScaleValue,
                                            (size.height - ((self.captionsTextView.frame.origin.y - 20) * yScaleValue)),
                                            CGRectGetWidth(self.captionsTextView.frame) * xScaleValue,
                                            (CGRectGetHeight(self.captionsTextView.frame)+20) * yScaleValue)

        captionsOverlayLayer.masksToBounds = true

        ///////////////////
        //TagFilter Layer//
        ///////////////////
        UIGraphicsBeginImageContext(self.tagView.bounds.size)
        self.tagView.layer.renderInContext(UIGraphicsGetCurrentContext()!)

        let tagViewImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        let tagOverlayLayer = CALayer()
        tagOverlayLayer.contents = tagViewImage.CGImage
        tagOverlayLayer.frame = CGRectMake(self.tagView.frame.origin.x * xScaleValue,
                                           (size.height - ((self.tagView.frame.origin.y) * yScaleValue)),
                                           CGRectGetWidth(self.tagView.frame) * xScaleValue,
                                           (CGRectGetHeight(self.tagView.frame)) * yScaleValue)
        ///////////////////////////////////////////////

        parentLayer.addSublayer(videoLayer)
        parentLayer.addSublayer(captionsOverlayLayer)
        parentLayer.addSublayer(tagOverlayLayer)

        ///////////////
        //Composition//
        ///////////////
        composition.animationTool = AVVideoCompositionCoreAnimationTool.init(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)
    }

我很乐意帮忙:)

2 个答案:

答案 0 :(得分:1)

如果问题是图层显示的图像的分辨率,则:

  • 这是你的第一个错误:UIGraphicsBeginImageContext。始终致电UIGraphicsBeginImageContextWithOptions和第三个0参数,或明确决议。

  • 您的第二个错误是您从未设置过CALayer contentsScale

答案 1 :(得分:1)

最后,我想在视频中添加UIViews UIView UIView。从那个VideoSaver.saveVideo(videoAsset!) { (composition, size) in //////////////// //Parent Layer// //////////////// let parentLayer = CALayer() parentLayer.frame = CGRectMake(0, 0, size.width, size.height) /////////////// //Video Layer// /////////////// let videoLayer = CALayer() videoLayer.frame = CGRectMake(0, 0, size.width, size.height) /////////////////////// //Effect Layout Layer// /////////////////////// UIGraphicsBeginImageContextWithOptions(self.effectLayoutView.bounds.size, false, 0.0) self.effectLayoutView.layer.renderInContext(UIGraphicsGetCurrentContext()!) let effectLayoutViewImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() let effectOverlayLayer = CALayer() effectOverlayLayer.contents = effectLayoutViewImage.CGImage effectOverlayLayer.frame = CGRectMake(0, 0, size.width, size.height) effectOverlayLayer.masksToBounds = true /////////////////////////////////////////////// parentLayer.addSublayer(videoLayer) parentLayer.addSublayer(effectOverlayLayer) /////////////// //Composition// /////////////// composition.animationTool = AVVideoCompositionCoreAnimationTool.init(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer) } 我创建图层并将其缩放到视频大小。如果有人遇到同样的问题,有一个代码:

component_item_id