Iphone get photo resolution programmatically

2019-09-17 15:19发布

问题:

There's a way to get programmatically the dimensions of a photo taken with an iphone? I want to take the dimensions directly without passing throught model version,so this solution can't be valid:

check model version->write a dictionary with dimensions of each iphone model -> take the correct index

回答1:

When you take a picture, you get an UIImage.

UIImage objects have a -(CGSize)size method that returns the dimensions of the image in points. You should multiply it by it's scale property to get pixels.

Source

ProTip: Read the documentation.



回答2:

Try this code for get maximum cameras resolution:

- (CMVideoDimensions) getCameraMaxStillImageResolution:(AVCaptureDevicePosition) cameraPosition {

    CMVideoDimensions max_resolution;
    max_resolution.width = 0;
    max_resolution.height = 0;

    AVCaptureDevice *captureDevice = nil;

    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices) {
        if ([device position] == cameraPosition) {
            captureDevice = device;
            break;
        }
    }
    if (captureDevice == nil) {
        return max_resolution;
    }

    NSArray* availFormats=captureDevice.formats;

    for (AVCaptureDeviceFormat* format in availFormats) {
        CMVideoDimensions resolution = format.highResolutionStillImageDimensions;
        int w = resolution.width;
        int h = resolution.height;
        if ((w * h) > (max_resolution.width * max_resolution.height)) {
            max_resolution.width = w;
            max_resolution.height = h;
        }
    }

    return max_resolution;
}

- (void) printCamerasInfo {
    CMVideoDimensions res;
    res = [self getCameraMaxStillImageResolution:AVCaptureDevicePositionBack];
    NSLog(@" Back  Camera max Image resolution: %d x %d", res.width, res.height);
    res = [self getCameraMaxStillImageResolution:AVCaptureDevicePositionFront];
    NSLog(@" Front Camera max Image resolution: %d x %d", res.width, res.height);
}