需要更改CALayer类对象的大小和位置 [英] Need to change size and location of CALayer class objects

查看:63
本文介绍了需要更改CALayer类对象的大小和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试复制Apple提供的名为AVCam的示例应用程序的所有功能:

I am trying to copy all of the functionality of this example app provided by Apple called AVCam: https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112-Intro-DontLinkElementID_2

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

The only thing I am having trouble with is changing the size and location of a UIView object. The problem is I am using apple's sample code and it's just not making sense for me.

Apple提供了这个示例VC,我将其称为MediaCapturePreviewView.这是头文件代码:

Apple provides this sample VC that I have called MediaCapturePreviewView. Here is the header file code:

#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

然后,在我应用程序的中央View Controller中,导入了"MediaCapturePreviewView"头文件.我刚刚给你看的最后但并非最不重要的一点,在中央视图控制器的主文件中,我在界面区域中有一个IBOutlet,如下所示:

Then in my app's central View Controller, I have imported the header file of "MediaCapturePreviewView" that I just showed you. Last but not least, in my central view controller's main file I have an IBOutlet in the interface area that looks like this:

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

以上IBOutlet已连接到Interface Builder中覆盖整个iPhone屏幕的UIView对象.

The above IBOutlet has been connected to a UIView object in Interface Builder that covers the entire iPhone screen.

这是当您点击拍照"时如何使用previewView的一个小示例.按钮:

And then here is a small example of how previewView is used when you tap the "take a picture" button:

- (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]];

我已经尝试在上述方法调用之后立即添加此代码,但这无济于事:

I have tried adding in this code right after the above method calls but it is not helping:

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

我知道您可以编辑CALayer类对象的属性,例如框架,边界和位置,但是我只是被卡住了,不知道我到底需要在哪里编辑这些东西.

I know that you can edit properties for CALayer class objects like frame, bounds, and position but I'm just stuck and don't know where exactly I need to edit these things.

这是拍照时当前UIView的外观:

This is what the current UIView looks like when taking a picture:

这就是我需要的样子:

我基本上需要它完全占据iPhone屏幕的前50%.

I basically need it to take up exactly the top 50% of the iPhone's screen.

非常感谢您的帮助.

推荐答案

视频具有适合iPhone屏幕的默认捕获比率.如果尝试将其缩放到其他比例(例如半屏),则会得到失真的图像或裁剪的图像.

The video has a default capture ratio which fits the iPhone's screen. If you try to scale it to a different ratio (such as half screen) you will get a distorted image or a cropped one.

为了实现您要执行的操作,我认为使用包含所需控件(例如拍照按钮)的 UIView 覆盖屏幕的一半会更容易.),然后使用以下方法将捕获的全屏图像裁剪为其一半大小:

In order to achieve what you're trying to do I think it would be easier to just cover up half of the screen with a UIView containing the controls you want (such as take a photo button) and then just crop the captured full screen image to half its size with something like this:

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);

这篇关于需要更改CALayer类对象的大小和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆