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