图像裁剪AVCaptureSession图像

问题描述:

所以我一整天都没有运气,它不必说很沮丧,我查了很多例子和可下载的类别,都宣称能够完美地裁剪图像。他们这样做,然而,当我尝试从通过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

看来,这个小难题并不只有我将近一周后,难倒,但只有少数人看过我的问题也没有建议。对于这个特殊的问题,我必须要说,我不能这样做,我沉吟,思考和思考了一段时间无济于事。直到我这样做

[self HideElements]; 

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

[self ShowElements]; 

就是这样,代码少,它的工作几乎瞬间。因此,不是试图通过滚动视图裁剪图像,而是在当时拍摄屏幕截图,然后使用滚动视图框架变量裁剪图像。隐藏/显示元素功能隐藏我想要的图片上的任何重叠元素。

+0

我面临同样的问题。你能告诉我在哪里需要调用这段代码片段?我会很感激。 –

+0

这方面的任何更新?我有完全一样的问题。看起来,截图的质量不会像从AV捕获会话中获得的照片那么好。谢谢。 – seenickcode