在显示图层之前应用CGAffineTransform [英] Apply CGAffineTransform prior to layer being displayed
问题描述
我正在尝试缩放 AVCaptureVideoPreviewLayer
,以便它从相机显示放大预览;我可以使用 setAffineTransform:CGAffineTransformMakeScale(2.0f,2.0f)
来完成这项工作,但只有在屏幕上显示该图层时才能使用。也就是说,除非我在回调期间(在$ code> viewDidAppear $)内调用它(在 CATransaction
中),否则比例变换似乎不会生效c $ c>。
I'm trying to scale an AVCaptureVideoPreviewLayer
so it displays a "zoomed in" preview from the camera; I can make this work using setAffineTransform:CGAffineTransformMakeScale(2.0f, 2.0f)
, but only once the layer is visible on screen. That is, the scale transform doesn't seem to take effect unless I call it (from within a CATransaction
) during a callback like viewDidAppear
.
我真正想做的是将比例变换应用于之前,使图层变得可见 - 例如在我初始化它并将其添加到 UIView
时 - 但我所有这样做的尝试都证明是徒劳的。如果我在此时设置了变换,然后在可见时尝试将值读回,它似乎确实具有所需的缩放变换集 - 但它对视图没有任何影响。我是iOS编程新手,所以我确信这里有一些基本的东西。任何帮助表示赞赏!
What I'd really like to do is have the scale transform be applied prior to the layer becoming visible--e.g. at the time that I initialize it and add it to the UIView
--but all my attempts to do so have proven futile. If I set the transform at this point, and then attempt to read the value back once it is visible, it does appear to have the desired scale transform set -- yet it does not have any effect on the view. I'm new to iOS programming, so I'm sure there's something basic I'm missing here; any help is appreciated!
推荐答案
两种简单的替代方法,一种(更好)适用于iOS 7.x,另一种适用于早期版本iOS:
Two simple alternative approaches, one (better) for iOS 7.x and one for earlier versions of iOS:
float zoomLevel = 2.0f;
if ([device respondsToSelector:@selector(setVideoZoomFactor:)]
&& device.activeFormat.videoMaxZoomFactor >= zoomLevel) {
// iOS 7.x with compatible hardware
if ([device lockForConfiguration:nil]) {
[device setVideoZoomFactor:zoomLevel];
[device unlockForConfiguration];
}
} else {
// Lesser cases
CGRect frame = captureVideoPreviewLayer.frame;
float width = frame.size.width * zoomLevel;
float height = frame.size.height * zoomLevel;
float x = (frame.size.width - width)/2;
float y = (frame.size.height - height)/2;
captureVideoPreviewLayer.bounds = CGRectMake(x, y, width, height);
}
这篇关于在显示图层之前应用CGAffineTransform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!