gpt4 book ai didi

ios - 在裁剪之前调整 UIImage 或 CIImage 的大小会拉伸(stretch)图像吗?

转载 作者:行者123 更新时间:2023-11-29 04:40:21 25 4
gpt4 key购买 nike

好吧,我已经尝试了所有我能找到的例子来做到这一点,遗憾的是大多数都是 2008-2009 年的,而 iOS5 似乎非常不同。我只是想调整大小和图像,以便短边是我指定的尺寸,并且保持比例。

我正在使用 AVFoundation 从相机中获取图像,我通过 UIImage 将其转换为 CIImage,这样我就可以应用滤镜并摆弄它,然后再转换回 UIImage 进行保存。

- (void) startCamera {

session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetPhoto;

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = _cameraView.bounds;
[_cameraView.layer addSublayer:captureVideoPreviewLayer];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
//no cam, handle error - need to put up a nice happy note here
NSLog(@"ERROR: %@", error);
}

[session addInput:input];

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

[session addOutput:stillImage];
[session startRunning];
}

- (IBAction) _cameraButtonPress:(id)sender {

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

[stillImage captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *startImage = [[UIImage alloc] initWithData:imageData];

//resizing of image to take place before anything else
UIImage *image = [startImage imageScaledToFitSize:_exportSSize]; //resizes to the size given in the prefs

//change the context to render using cpu, so on app exit renders get completed
context = [CIContext contextWithOptions:
[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:kCIContextUseSoftwareRenderer]];

//set up the saving library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

//create a new ciimage
CIImage *greyImage = [[CIImage alloc] initWithCGImage:[image CGImage]];

//create a CIMonochrome filter
desaturate = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, greyImage, @"inputIntensity", [NSNumber numberWithFloat:0.00], nil];

//[crop setValue:ciImage forKey:@"inputImage"];
//[crop setValue:[CIVector vectorWithX:0.0f Y:0.0f Z:_exportSize W:_exportSize] forKey:@"inputRectangle"];

CIImage *croppedColourImage = [desaturate valueForKey:@"outputImage"];
//convert it to a cgimage for saving
CGImageRef cropColourImage = [context createCGImage:croppedColourImage fromRect:[croppedColourImage extent]];

//saving of the images
[library writeImageToSavedPhotosAlbum:cropColourImage metadata:[croppedColourImage properties] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(@"ERROR in image save: %@", error);
} else
{
NSLog(@"SUCCESS in image save");
//in here we'll put a nice animation or something
CGImageRelease(cropColourImage);
}

}];
}];
}

此代码是测试代码,因此其中会有各种尝试,对此表示歉意。

我得到的最接近的是使用 Matt Gemmell 的代码 here

但是,无论我如何尝试,总是会拉伸(stretch)图像。我想调整图像大小,然后将其裁剪为 512 像素的正方形。如果我只使用 CICrop 滤镜,它会占用左上角 512 像素,因此我会丢失很多图像(抓取高分辨率照片,因为稍后我还想导出 1800x1800 图像)。我的想法是先调整图像大小,然后裁剪。但无论我做什么,图像都会被拉伸(stretch)。

所以我的问题是,在 iOS5+ 中是否有适当建议的公认方法来调整大小和图像?

谢谢。

最佳答案

我找到了一种方法:

UIGraphicsBeginImageContext( newSize );

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

关于ios - 在裁剪之前调整 UIImage 或 CIImage 的大小会拉伸(stretch)图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486244/

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