gpt4 book ai didi

ios - 使用 AVCaptureSession 预设时如何更改图像质量

转载 作者:行者123 更新时间:2023-12-01 17:55:20 25 4
gpt4 key购买 nike

我正在寻找有关 AVFoundation 的帮助。目前,我已经按照有关如何从此主题捕获静止图像的分步指南进行操作:How to save photos taken using AVFoundation to Photo Album?

我的问题是,如何降低我保存的图像的质量以及使用 AVCaptureSessionPreset640x480。我想将图像质量减半,或者如果有另一种方法可以使保存的图像尽可能小(可能是 320x280),那么这可能比调整实际质量更好。

我不知道以前是否有人问过这个问题,但过去几天我一直在网上搜索但找不到答案。下面是我的代码。

`

#import "ViewController.h"
#import <ImageIO/ImageIO.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize imagePreview;
@synthesize iImage;
@synthesize stillImageOutput;

-(IBAction)captureNow {
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection)
{
break;
}
}

NSLog(@"about to request a capture from: %@", stillImageOutput);
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments)
{
// Do something with the attachments.
NSLog(@"attachements: %@", exifAttachments);
} else {
NSLog(@"no attachments");
}

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
//NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 0.5f)]];
UIImage *image = [[UIImage alloc] initWithData:imageData];

self.iImage.image = image;

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}];
}

-(void)viewDidAppear:(BOOL)animated
{
AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPreset640x480;

CALayer *viewLayer = self.imagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = self.imagePreview.bounds;
[self.imagePreview.layer addSublayer:captureVideoPreviewLayer];

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
[session addInput:input];

stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];

[session startRunning];
}


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

`

最佳答案

尝试使用不同的预设,这将为您提供不同分辨率的图像。

根据 Apple 文档,对于 iPhone4(背面),您将获得该 session 的以下预设的以下分辨率图像。

AVCaptureSessionPresetHigh : 1280x720

AVCaptureSessionPresetMedium : 480x360

AVCaptureSessionPresetLow : 192x144

AVCaptureSessionPreset640x480 : 640x480

AVCaptureSessionPreset1280x720 : 1280x720

AVCaptureSessionPresetPhoto : 2592x1936.这不支持视频输出

希望这会有所帮助。

关于ios - 使用 AVCaptureSession 预设时如何更改图像质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19002257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com