2012-10-06 226 views
1

所以我一整天都沒有運氣,它不必說很沮喪,我查了很多例子和可下載的類別,都宣稱能夠完美地裁剪圖像。他們這樣做,然而,當我嘗試從通過AVCaptureSession生成的圖像執行它時,它不起作用。我諮詢這兩種來源圖像裁剪AVCaptureSession圖像

http://codefuel.wordpress.com/2011/04/22/image-cropping-from-a-uiscrollview/

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

,並從第一個鏈接的項目似乎爲標榜,但只要我本事做一個AV捕捉圖像上相同的魔術,直接工作。 ..沒有...

有沒有人有這方面的見解?這裏也是我的代碼供參考。

- (IBAction)TakePhotoPressed:(id)sender 
{ 
    AVCaptureConnection *videoConnection = nil; 
    for (AVCaptureConnection *connection in stillImageOutput.connections) 
    { 
    for (AVCaptureInputPort *port in [connection inputPorts]) 
    { 
    if ([[port mediaType] isEqual:AVMediaTypeVideo]) 
    { 
    videoConnection = connection; 
    break; 
    } 
    } 
    if (videoConnection) { break; } 
    } 

    //NSLog(@"about to request a capture from: %@", stillImageOutput); 
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) 
    { 
     CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); 
     if (exifAttachments) 
     { 
      // Do something with the attachments. 
      //NSLog(@"attachements: %@", exifAttachments); 
     } 
     else 

     NSLog(@"no attachments"); 

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
     UIImage *image = [[UIImage alloc] initWithData:imageData]; 

     NSLog(@"%f",image.size.width); 
     NSLog(@"%f",image.size.height); 


     float scale = 1.0f/_scrollView.zoomScale; 

     CGRect visibleRect; 
     visibleRect.origin.x = _scrollView.contentOffset.x * scale; 
     visibleRect.origin.y = _scrollView.contentOffset.x * scale; 
     visibleRect.size.width = _scrollView.bounds.size.width * scale; 
     visibleRect.size.height = _scrollView.bounds.size.height * scale; 

     UIImage* cropped = [self cropImage:image withRect:visibleRect]; 

     [croppedImage setImage:cropped];   

     [image release]; 
    } 
     ]; 

    [croppedImage setHidden:NO]; 


} 

上面使用的cropImage函數。

-(UIImage*)cropImage :(UIImage*)originalImage withRect :(CGRect) rect 
{ 


    CGRect transformedRect=rect; 
    if(originalImage.imageOrientation==UIImageOrientationRight) 
    { 
     transformedRect.origin.x = rect.origin.y; 
     transformedRect.origin.y = originalImage.size.width-(rect.origin.x+rect.size.width); 
     transformedRect.size.width = rect.size.height; 
     transformedRect.size.height = rect.size.width; 
    } 

    CGImageRef cr = CGImageCreateWithImageInRect(originalImage.CGImage, transformedRect); 
    UIImage* cropped = [UIImage imageWithCGImage:cr scale:originalImage.scale orientation:originalImage.imageOrientation]; 
    [croppedImage setFrame:CGRectMake(croppedImage.frame.origin.x, 
             croppedImage.frame.origin.y, 
             cropped.size.width, 
             cropped.size.height)]; 

    CGImageRelease(cr); 
    return cropped; 
} 

我也動心了冗長和武裝的任何人可以幫助我在我的困境與儘可能多的信息可以張貼我的我的滾動視圖和avcapture會話中運行init。但是,這可能有點太多,所以如果你想看到它只是問。

現在對於什麼樣的代碼實際上做的結果嗎?..

它看起來像什麼我拍攝照片之前

before shot

後...

after shot

編輯:

嗯,我現在有幾個意見,沒有評論,所以要麼沒有人知道它或它是如此簡單,他們認爲我會再次想出來......在任何情況下,我還沒有取得任何進展。因此,對於任何有興趣在這裏是用一小段代碼示例應用程序的所有設置,你可以看到我在做什麼

https://docs.google.com/open?id=0Bxr4V3a9QFM_NnoxMkhzZTVNVEE

回答

3

看來,這個小難題並不只有我將近一週後,難倒,但只有少數人看過我的問題也沒有建議。對於這個特殊的問題,我必須要說,我不能這樣做,我沉吟,思考和思考了一段時間無濟於事。直到我這樣做

[self HideElements]; 

UIGraphicsBeginImageContext(chosenPhotoView.frame.size); 
[chosenPhotoView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

[self ShowElements]; 

就是這樣,代碼少,它的工作幾乎瞬間。因此,不是試圖通過滾動視圖裁剪圖像,而是在當時拍攝屏幕截圖,然後使用滾動視圖框架變量裁剪圖像。隱藏/顯示元素功能隱藏我想要的圖片上的任何重疊元素。

+0

我面臨同樣的問題。你能告訴我在哪裏需要調用這段代碼片段?我會很感激。 –

+0

這方面的任何更新?我有完全一樣的問題。看起來,截圖的質量不會像從AV捕獲會話中獲得的照片那麼好。謝謝。 – seenickcode