http://stackoverflow.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. objcopy

    objcopy objcopy [options] infile [outfile] Copy the contents of the input object file to another fil ...

  2. 使用hibernate annotation 为非空列加上默认值

    在网上查了很多资料都没找到如何为非空列加上默认值 以前的做法是给字段一个初始值,加上dynamic-insert属性 换了annotation了以后没有找到如何设置dynamic-insert属性 但 ...

  3. java中的clone

    .clone 要实现cloneable接口: .深度clone和浅度clone .对象.clone() 1. Clone&Copy      假设现在有一个Employee对象,Employe ...

  4. UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)

    题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x ...

  5. UVa 557 (概率 递推) Burger

    题意: 有两种汉堡给2n个孩子吃,每个孩子在吃之前要抛硬币决定吃哪一种汉堡.如果只剩一种汉堡,就不用抛硬币了. 求最后两个孩子吃到同一种汉堡的概率. 分析: 可以从反面思考,求最后两个孩子吃到不同汉堡 ...

  6. Oracle数据文件管理

    1.数据文件概述 Oracle数据库的数据文件(扩展名为DBF的文件)是用于保存数据库中数据的文件,系统数据.数据字典数据.临时数据.索引数据.应用数据等都物理地存储在数据文件中.用户对数据库中数据的 ...

  7. 《C++ Primer 4th》读书笔记 第3章-标准库类型

        原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3911534.html

  8. QuartZ Cron表达式在java定时框架中的应用

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表. CronT ...

  9. 总结mysql服务器查询慢原因与解决方法

    本文针对MySQL数据库服务器查询逐渐变慢的问题, 进行分析,并提出相应的解决办法,具体的分析解决办法如下: 会经常发现开发人员查一下没用索引的语句或者没有limit n的语句,这些没语句会对数据库造 ...

  10. UI篇--布局问题

    1.android:layout_marginRight 不起作用解决方法 今天想在RelativeLayout的左右分别放上一个按钮, 左边按钮用marginLeft="10dp" ...