需要更改CALayer类对象的大小和位置

我试图复制苹果提供的这个示例应用程序的所有function称为AVCam: https : //developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid / DTS40010112-简介-DontLinkElementID_2

我唯一遇到的问题是更改UIView对象的大小和位置。 问题是我正在使用苹果的示例代码,这只是对我来说没有意义。

苹果公司提供了这个示例VC,我称之为MediaCapturePreviewView。 这是头文件代码:

#import <UIKit/UIKit.h> @class AVCaptureSession; @interface MediaCapturePreviewView : UIView @property (nonatomic) AVCaptureSession *session; @end 

这是它的主要文件代码:

 #import "MediaCapturePreviewView.h" #import <AVFoundation/AVFoundation.h> @implementation MediaCapturePreviewView - (void)drawRect:(CGRect)rect { // Drawing code } */ + (Class)layerClass { return [AVCaptureVideoPreviewLayer class]; } - (AVCaptureSession *)session { return [(AVCaptureVideoPreviewLayer *)[self layer] session]; } - (void)setSession:(AVCaptureSession *)session { [(AVCaptureVideoPreviewLayer *)[self layer] setSession:session]; } @end 

然后在我的应用程序的中央视图控制器,我已经导入了“MediaCapturePreviewView”的头文件,我只是给你看。 最后但并非最不重要的是,在我的中央视图控制器的主文件中,我在界面区域中有一个IBOutlet,如下所示:

 @property (nonatomic, weak) IBOutlet MediaCapturePreviewView *previewView; 

上面的IBOutlet已经连接到Interface Builder中的UIView对象,覆盖了整个iPhone屏幕。

接下来是点击“拍照”button时如何使用previewView的一个小例子:

 - (IBAction)snapStillImage:(id)sender { dispatch_async([self sessionQueue], ^{ // Update the orientation on the still image output video connection before capturing. [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]]; 

上面的方法调用之后,我尝试在这个代码中添加,但是它并没有帮助:

 _previewView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.frame.size.height/2); 

我知道你可以编辑像框架,边界和位置的CALayer类对象的属性,但我只是卡住,不知道我到底需要编辑这些东西。

这就是当前UIView在拍照时的样子:

在这里输入图像说明

这就是我所需要的样子:

在这里输入图像说明

我基本上需要占据iPhone屏幕的前50%。

任何帮助非常感谢你。

该video具有适合iPhone屏幕的默认捕捉比例。 如果您尝试将其缩放到不同的比例(例如半屏),您将会看到扭曲的图像或裁切的图像。

为了实现你想要做的事情,我想用一个包含你想要的控件的UIView (比如拍一个照片button)掩盖一半的屏幕会更容易,然后裁剪捕获的全屏图像到一半的大小,像这样的东西:

 CGRect clippedRect = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height/2.0); CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect); UIImage *newImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef);