简体   繁体   English

补偿AVLayerVideoGravityResizeAspectFill高度差

[英]Compensate For AVLayerVideoGravityResizeAspectFill Height Difference

I have a nested video like this: Live camera feed 我有一个这样的嵌套视频: 实时摄像机供稿

When the user takes a photo, the image is offset along the y axis Captured Still image 用户拍照时,图像沿y轴偏移捕获的静止图像

I do want to capture the WHOLE image and let the user scroll up and down. 我确实想捕获整个图像,并让用户上下滚动。 They can do this currently but I want the starting scroll of the image to be centered to match the camera feed preview. 他们目前可以执行此操作,但是我希望图像的起始滚动条居中以与相机Feed预览相匹配。 So if they take a picture, the image matches the frame that the video feed was showing. 因此,如果他们拍照,则图像与视频供稿显示的帧相匹配。

The problem is, because the aspect on the camera is set to AVLayerVideoGravityResizeAspectFill it's doing some 'cropping' to fit the image into the live preview. 问题是,因为相机上的外观设置为AVLayerVideoGravityResizeAspectFill,所以它正在进行一些“裁剪”以使图像适合实时预览。 Since the height is much bigger than the width, there are top and bottom parts that are captured in the image that are NOT showing up in the live feed (naturally). 由于高度远大于宽度,因此图像中捕获的顶部和底部部分没有(自然地)显示在实时供稿中。

What I don't know, however, is how much the top is being cropped so I can offset the previewed image to match this. 但是,我不知道要裁剪多少顶部,因此我可以偏移预览图像以匹配此图像。

So my question is: Do you know how to calculate how much is being cropped from the top of a camera with its aspect ratio set to AVLayerVideoGravityResizeAspectFill? 所以我的问题是:您是否知道如何计算将宽高比设置为AVLayerVideoGravityResizeAspectFill的相机顶部裁剪多少? (Objective-C and Swift answers welcome!) (欢迎使用Objective-C和Swift答案!)

The solution I came up with is this: 我想出的解决方案是这样的:

  func getVerticalOffsetAdjustment()->CGFloat
    {
        var cropRect:CGRect = _videoPreviewLayer.metadataOutputRectOfInterestForRect(_videoPreviewLayer.bounds) //returns the cropped aspect ratio box so you can use its offset position
        //Because the camera is rotated by 90 degrees, you need to use .x for the actual y value when in portrait mode
        return cropRect.origin.x/cropRect.width * frame.height
    }

Its confusing I admit, but because the camera is rotated 90 degrees when in portrait mode you need to use the width and x values. 我承认这令人困惑,但是由于在纵向模式下相机旋转了90度,因此您需要使用width和x值。 The cropRect will return a value like (0.125,0,0,75,1.0)(your exact values will be different). cropRect将返回类似(0.125,0,0,75,1.0)的值(您的确切值将有所不同)。

What this tells me, is my my shifted y value (that the video live feed is showing me) is shifted down 12.5% of its total height and that the height of the video feed is only 75% of the total height. 这告诉我的是,我的y值(视频实时供稿正在向我显示)下移了其总高度的12.5%,而视频供稿的高度仅为总高度的75%。

So I take 12.5% and divide by 75% to get the normalized (to my UIWindow) value and then apply that amount to the scrollview offset. 因此,我取12.5%的值除以75%的值以获取归一化的值(至UIWindow),然后将该值应用于scrollview偏移量。

WHEW!!! 噢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AVLayerVideoGravityResizeAspectFill在哪里裁剪 - Where does AVLayerVideoGravityResizeAspectFill crop 将 videoGravity 设置为 AVLayerVideoGravityResizeAspectFill 对 iOS 5.0 没有影响 - Setting videoGravity to AVLayerVideoGravityResizeAspectFill has no effect on iOS 5.0 当videoGravity设置为AVLayerVideoGravityResizeAspectFill时,AVPlayerLayer视频“溢出” - AVPlayerLayer video “overspilled” when videoGravity set to AVLayerVideoGravityResizeAspectFill UIAlertController高度差 - UIAlertController height difference UITabBar Wierd高度差 - UITabBar Wierd Height Difference iOS CoreMotion-计算高度差 - iOS CoreMotion - Calculating Height Difference 模拟AVLayerVideoGravityResizeAspectFill:裁剪和居中视频以模仿预览而不会丢失锐度 - Simulate AVLayerVideoGravityResizeAspectFill: crop and center video to mimic preview without losing sharpness 如何补偿智能手机屏幕的延迟? - How do I compensate for the delay of smartphone screens? iOS模拟器的窗口高度差异介于7.1和8.3之间 - iOS simulator window height difference between 7.1 and 8.3 内在大小与系统宽度/高度约束的区别是什么? - What is the difference of intrinsic size vs system width/height constraints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM