获取iPhone的相机分辨率?

时间:2009-10-28 15:51:43

标签: iphone camera resolution detect iphone-3gs

有没有办法获得iPhone相机的分辨率?显然3G有1200x1600,3GS有1500x2000,但我如何从我的代码中获取这些值(没有拍照)。我需要他们对相机预览做一些仿射变换。

我可以检测到它是3G还是3GS来硬编码这些值,但这只是最后的选择。

6 个答案:

答案 0 :(得分:7)

我认为你的“最后手段”是一个很好的解决方案。

检测模型有什么问题?对于这些型号中的任何一种,硬件都不会改变。你可能唯一担心的是3GSX-R或16mpx相机。此时您可能还需要更新您的应用程序,并且可以在列表中添加其他值。

我投票支持模特检测。

答案 1 :(得分:3)

帖子很旧,但它几乎是谷歌中的第一个,并且它没有有效的答案,所以这里还有一个选项:

从4.x到7.x的iOS解决方案

这完全是AV Foundation框架

配置并启动AVCaptureSession后,您可以在[[[session.inputs.lastObject] ports].lastObject formatDescription]变量

中找到视频尺寸

这是近似代码:

AVCaptureSession* session = ...;
AVCaptureDevice *videoCaptureDevice = ...;
AVCaptureDeviceInput *videoInput = ...;

[session beginConfiguration];
if ([session canAddInput:videoInput]) {[session addInput:videoInput];}
[session commitConfiguration];
[session startRunning];

//this is the clue
AVCaptureInputPort *port = videoInput.ports.lastObject;
if ([port mediaType] == AVMediaTypeVideo)
{
    videoDimensions = CMVideoFormatDescriptionGetDimensions([port formatDescription]);
}

iOS8解决方案

Apple确实再次更改了所有内容:现在您必须订阅AVCaptureInputPortFormatDescriptionDidChangeNotification

以下是样本:

-(void)initSession
{
    AVCaptureSession* session = ...;
    AVCaptureDevice *videoCaptureDevice = ...;
    AVCaptureDeviceInput *videoInput = ...;

    [session beginConfiguration];
    if ([session canAddInput:videoInput]) {[session addInput:videoInput];}
    [session commitConfiguration];
    [session startRunning];

    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(avCaptureInputPortFormatDescriptionDidChangeNotification:)
            name:@"AVCaptureInputPortFormatDescriptionDidChangeNotification"
            object:nil];
}

-(void)avCaptureInputPortFormatDescriptionDidChangeNotification:(NSNotification *)notification
{
    AVCaptureInputPort *port = [videoInput.ports objectAtIndex:0];
    CMFormatDescriptionRef formatDescription = port.formatDescription;
    if (formatDescription) {
        videoDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
    }

}

答案 2 :(得分:2)

你真的需要知道图像的分辨率来设置仿射变换吗?由于视图已预先设置为覆盖屏幕的宽度,因此您可以根据要占用的屏幕分数来更改变换,即如果您需要预览为160像素,则只需将其缩小默认值为50%。我现在正在应用程序中执行此操作,它适用于新旧iPhone ......

答案 3 :(得分:0)

由于Apple不允许您直接与硬件通信,因此合法App Store产品的选择并不多。

答案 4 :(得分:0)

虽然我认为窃笑的答案肯定是正确的。我以为我会发布这个很有趣。

你可以在iPhone上查看用户存储的相册(我正在大胆地假设这可以在iPhone上以编程方式完成而不受限制!!)假设手机上的照片大部分都是用打电话给自己,然后你可以计算出平均照片分辨率是多少,并随之滚动。

答案 5 :(得分:0)

你为什么不想拍照?使用UIImagePickerController的 takePicture 方法,指示用户这是必要的校准步骤。之后,查看图片的分辨率,持久保存该值,并删除您拍摄的图片。之后,您将拥有自己的分辨率,并能够对其进行转换。