Why does capturing images with AVFoundation give me 480x640 images when the preset is 640x480?

后端 未结 1 1332
心在旅途
心在旅途 2021-02-04 16:23

I have some pretty basic code to capture a still image using AVFoundation.

AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[s         


        
1条回答
  •  心在旅途
    2021-02-04 17:14

    This happens because the orientation set in the metadata of the new image is being affected by the orientation of the AV system that creates it. The layout of the actual image data is, of course, different from the orientation mentioned in your metadata. Some image viewing programs respect the metadata orientation, some ignore it.

    You can affect the metadata orientation of the AV system by calling:

    AVCaptureConnection *videoConnection = ...;
    if ([videoConnection isVideoOrientationSupported])
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationSomething];
    

    You can affect the metadata orientation of a UIImage by calling:

    UIImage *rotatedImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:1.0f orientation:UIImageOrientationSomething];
    

    But the actual data from the AVCapture system will always appear with the wider dimension as X and the narrower dimension as Y, and will appear to be oriented in LandscapeLeft.

    If you want the actual data to line up with what your metadata claims, you need to modify the actual data. You can do this by writing the image out to a new image using CGContexts and AffineTransforms. Or there is an easier workaround. Use the UIImage+Resize package as discussed here. And resize the image to it's current size by calling:

    UIImage *rotatedImage = [image resizedImage:CGSizeMake(image.size.width, image.size.height) interpolationQuality:kCGInterpolationDefault];
    

    This will rectify the data's orientation as a side effect.

    If you don't want to include the whole UIImage+Resize thing you can check out it's code and strip out the parts where the data is transformed.

    0 讨论(0)
提交回复
热议问题