Get iPhone's camera resolution?

前端 未结 6 710
再見小時候
再見小時候 2020-12-19 23:11

Is there any way to get the resolution of the iPhone\'s camera? Apparently the 3G has 1200x1600, and 3GS has 1500x2000, but how do I obtain these values from inside my code

6条回答
  •  醉话见心
    2020-12-19 23:40

    The post is old, but it is almost first in google, and it has got no valid answer, so here's one more option:

    Solution for iOS from 4.x to 7.x

    It's all in terms of AV Foundation framework

    After AVCaptureSession is configured and started you can find video dimensions inside [[[session.inputs.lastObject] ports].lastObject formatDescription] variable

    Here's approximate code:

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

    Solution for iOS8

    Apple did change everything again: now you must subscribe for AVCaptureInputPortFormatDescriptionDidChangeNotification

    Here is the sample:

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

提交回复
热议问题