Need to change size and location of CALayer class

2019-09-07 20:04发布

问题:

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

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

Here is it's main file code:

#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

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;

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

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

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.

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

And this is what I need it to look like:

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

Any help is greatly appreciated thank you.

回答1:

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.

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