0
好吧,我已经试过了我可以找到的所有例子,可悲的是,其中大部分都是从2008年到2009年,而iOS5似乎有很大的不同。我只是想调整大小和图像,以便短边是我指定的大小,并保持不变。在裁剪之前调整UIImage或CIImage的大小,拉伸图像?
我使用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);
}
}];
}];
}
此代码是测试代码,所以会有各种各样的尝试,为此道歉。
我有使用马特Gemmell代码here
最接近但是,无论我怎么努力,总是绵延的图像。我想调整图像大小,然后将其裁剪为512像素平方。如果我只是做一个CICrop过滤器,它需要左上512像素,所以我失去了很多图像(抓取高分辨率的照片,因为后来我还想导出1800x1800图像)。我的想法是先调整图像大小,然后裁剪。但是无论我做什么,图像都会变得很紧张。
所以我的问题是,有没有适当的建议公认的方式来调整大小和图像在iOS5 +?
谢谢。
我发现这样做的一种方式:UIGraphicsBeginImageContext(新尺寸); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();这可能不是我应该的方式,但它现在工作。想知道为什么苹果没有让这个更容易! :) – mrEmpty
我刚刚遇到了这个完全相同的问题,但您建议的模型似乎不适用于我。我在这里发布了这个问题http://stackoverflow.com/questions/10491080/uiimage-resizing-not-working-properly –
而且我在另一个线程中也提供了一个适用于我的解决方案(它可以在iOS 5中正常工作) :http://stackoverflow.com/questions/10491080/uiimage-resizing-not-working-properly/10491692#10491692 – Rob