2012-05-07 13 views
0

OK、私はこれを行うために見つけることができるすべての単一の例を試しました。悲しいことに、それらのほとんどは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 +で画像のサイズを変更してイメージを表示する方法が適切かどうかです。

ありがとうございました。

+0

私はそれを行うための一つの方法を発見した:UIGraphicsBeginImageContext(NewSizeパラメータを)。 [image drawInRect:CGRectMake(0,0、newSize.width、newSize.height)];UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();それは、私が想定している方法ではないかもしれませんが、今のところ働いています。なぜAppleがこれをより簡単にしなかったのだろうか? :) – mrEmpty

+0

私はちょうどあなたが提案しているモデルは私のために動作していないように、この全く同じ問題に遭遇した。私はここに問題を投稿しましたhttp://stackoverflow.com/questions/10491080/uiimage-resizing-not-working-properly –

+0

そして私は他のスレッドでも私のために働く解決策を提供しました(これはiOS 5でうまくいきます) :http://stackoverflow.com/questions/10491080/uiimage-resizing-not-working-properly/10491692#10491692 – Rob

答えて

1

私はそれを行うための一つの方法を発見した:

UIGraphicsBeginImageContext(newSize); 

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

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
関連する問題