2012-05-07 1 views
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); 
    } 

    }]; 
}]; 
} 

이 코드는 테스트 코드이므로 모든 종류의 시도가있을 것입니다. 사과드립니다.

나는 매트 겜멜의 코드 here

을 사용하고있어 가장 가까운 그러나 그것은, 내가 노력할 상관없이, 항상 이미지를 뻗어 없습니다. 이미지의 크기를 조정 한 다음 512 픽셀의 사각형으로 자르고 싶습니다. CICrop 필터를 사용하면 왼쪽 상단이 512 픽셀이므로 이미지를 많이 잃을 수 있습니다 (고해상도 사진을 찍어 나중에 1800x1800 이미지를 내보낼 수도 있음). 내 생각은 이미지의 크기를 먼저 조절 한 다음 자르기였다. 그러나 내가 무엇을해도 이미지가 뻗어납니다.

제 궁금한 점은, iOS5 +에서 크기를 조정하고 이미지를 표시하는 적절한 방법이 있습니까?

감사합니다.

+0

: UIGraphicsBeginImageContext (newSize와를); [이미지 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();