AVCaptureSession 照相时获取 AVCaptureVideoPreviewLayer尺寸

时间:2021-07-04 23:47:54

http://*.com/questions/14153878/avcapturesession-preset-photo-and-avcapturevideopreviewlayer-size

I initialize an AVCaptureSession and I preset it like this :

AVCaptureSession *newCaptureSession = [[AVCaptureSession alloc] init];
if (YES==[newCaptureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) {
    newCaptureSession.sessionPreset = AVCaptureSessionPresetPhoto;
} else {
    // Error management
}

Then I setup an AVCaptureVideoPreviewLayer :

self.preview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/*426*/)];
CALayer *previewLayer = preview.layer;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
captureVideoPreviewLayer.frame = previewLayer.frame;
[previewLayer addSublayer:captureVideoPreviewLayer];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;

My question is:
How can I get the exact CGSize needed to display all the captureVideoPreviewLayer layer on screen ? More precisely I need the height as AVLayerVideoGravityResizeAspect make the AVCaptureVideoPreviewLayer fits the preview.size ?
I try to get AVCaptureVideoPreviewLayer size that fit right.

Very thank you for your help
objective-c ios camera
share|improve this question

After some research with AVCaptureSessionPresetPhoto the AVCaptureVideoPreviewLayer respect the 3/4 ration of iPhone camera. So it's easy to have the right height with simple calculus.
As an instance if the width is 320 the adequate height is:
320*4/3=426.6
share|improve this answer

// Get your AVCaptureSession somehow. I'm getting mine out of self.videoCamera, which is a GPUImageVideoCamera
    // Get the appropriate AVCaptureVideoDataOutput out of the capture session. I only have one session, so it's easy.

AVCaptureVideoDataOutput *output = [[[self.videoCamera captureSession] outputs] lastObject];
    NSDictionary* outputSettings = [output videoSettings];

// AVVideoWidthKey and AVVideoHeightKey did not work. I had to use these literal keys.
    long width  = [[outputSettings objectForKey:@"Width"]  longValue];
    long height = [[outputSettings objectForKey:@"Height"] longValue];

// video camera output dimensions are always for landscape mode. Transpose if your camera is in portrait mode.
    if (UIInterfaceOrientationIsPortrait([self.videoCamera outputImageOrientation])) {
        long buf = width;
        width = height;
        height = buf;
    }

CGSize outputSize = CGSizeMake(width, height);

AVCaptureSession 照相时获取 AVCaptureVideoPreviewLayer尺寸的更多相关文章

  1. ios或者cocos2d-x开发在Xcode编译时自适应失效,获取屏幕尺寸不准确

    在cocos2d-x的开发中,发现之前很好使的 setDesignResolutionSize(960.0f, 640.0f, kResolutionExactFit)自适应不好用了,后来调试发现不是 ...

  2. Android计量单位px,in,mm,pt,dp,dip,sp和获取屏幕尺寸与密度

    ###########################################Android计量单位########################################### px ...

  3. iOS根据Url 获取图片尺寸

    iOS根据Url 获取图片尺寸 // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if([i ...

  4. (转载)iOS UILabel自定义行间距时获取高度

    本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...

  5. 根据Url 获取图片尺寸 iOS

    // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL {     NSURL* URL = nil;     if([imageURL ...

  6. iOS开发——运行时OC篇&使用运行时获取系统的属性:使用自己的手势修改系统自带的手势

    使用运行时获取系统的属性:使用自己的手势修改系统自带的手势 有的时候我需要实现一个功能,但是没有想到很好的方法或者想到了方法只是那个方法实现起来太麻烦,一或者确实为了装逼,我们就会想到iOS开发中最牛 ...

  7. 工具类 dp转px 获取图片实际尺寸 获取屏幕尺寸

    dp转px public class Dp2pxUtils { public static int Dp2Px(Context context, float dp) { final float sca ...

  8. iOS UILabel自定义行间距时获取高度

    本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...

  9. iOS开发——根据Url 获取图片尺寸

    转自:http://www.oschina.net/code/snippet_2248391_53038 // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:( ...

随机推荐

  1. Git使用小记

    刚刚简答的完成了pureblog,想着先上传导Github上去,等着以后有时间了在完善其功能,所以使用Git上传导Github代码仓库上去,这里简答的记录以下使用小计. 我们首先下载Git,我们使用用 ...

  2. 十五个常用的jquery代码段

    十五个常用的jquery代码段 回到顶部按钮 通过使用 jQuery 中的 animate 和 scrollTop 方法,你无需插件便可创建一个简单地回到顶部动画: 1 // Back to top ...

  3. Android-AsyncTask异步任务(获取手机联系人)

    本篇随笔将讲解一下Android的多线程的知识,以及如何通过AsyncTask机制来实现线程之间的通信. 一.Android当中的多线程 在Android当中,当一个应用程序的组件启动的时候,并且没有 ...

  4. mac eclipse 下安装subclipse

    参考 http://www.cnblogs.com/yinxiangpei/articles/3859057.html 推荐安装homebrew 在安装javahl时注意版本对应 http://sub ...

  5. GroupingView点击分组标题不展开,或点击标题部分文字不展开

    GroupingView结构:    分组标题groupTextTpl是用两个DIV 来进行修饰的,在mouseDown时,EXT会查找css class=".x-grid-group-hd ...

  6. caffe训练超参数

    错误: caffe % ./build/tools/caffe train -solver models/finetune_flickr_style/solver.prototxt -weights ...

  7. Ubuntu14.04上安装Composer

    1,查看机子上有没有安装php 2,下载Composer的安装包 3,安装Composer 4,设置Composer全局可访问

  8. setData方法修改data中对象或数组的属性值(小程序开发)

    今日在开发小程序地图的过程中,遇到一个问题,困扰了我一会 业务如下: 困扰点: 我不知道如何修改data中数组包含的对象是如何修改的:期初的想法还是想共享上面的数据,想的太简单了 正确的解决步骤: 直 ...

  9. thinkphp error:no database select

    配置正确,项目运行时确出现,no database selected . 解决方法: 需要清除 /App/Runtime  runtime~.php文件

  10. opencv2函数学习之erode、dilate:图像腐蚀和膨胀

    图像腐蚀和图像膨胀是图像中两种最基本形态学操作. ,-), ,int borderType=BORDER_CONSTANT, const Scalar& borderValue=morphol ...