好的,我已经尝试了我能找到的每一个例子,遗憾的是它们中的大多数都是从 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 的代码
但是,无论我尝试什么,总是会拉伸图像。我想调整图像大小,然后将其裁剪为 512 像素正方形。如果我只是做一个 CICrop 过滤器,它会占用左上角的 512 像素,所以我会丢失很多图像(抓取高分辨率照片,因为稍后我还想导出 1800x1800 图像)。我的想法是先调整图像大小,然后裁剪。但无论我做什么,图像都会被拉伸。
所以我的问题是,在 iOS5+ 中是否有适当的建议可识别方法来调整大小和图像?
谢谢。