2011-08-11 37 views
2

我的问题是最类似于此一:AVFoundation作物根据预览宽高比拍摄的静止图像

Cropping image captured by AVCaptureSession

我有一个使用AVFoundation用于拍摄静态图像的应用程序。我的AVCaptureVideoPreviewLayer具有AVLayerVideoGravityResizeAspectFill视频重力,从而使得向用户显示的预览图片可以从顶部和底部进行裁剪。

当用户按下“拍摄”按钮时,实际拍摄的图像与显示给用户的预览图像不同。我的问题是如何相应裁剪捕获的图像?

在此先感谢。

+0

捕获的图像不同于预览图像,因为您正在调整方向填充的大小,对吧?因此,为什么不改变属性到'AVLayerVideoGravityResizeAspect'或者调整预览图层的大小以匹配捕获图像的比例? – MiguelB

+0

您是否找到解决您的问题的方法? – Dejell

回答

0

我使用UIImage+Resize类别here提供了一些我写信来完成这项工作的新方法。我重新格式化了一些代码,看起来更好,没有测试过,但它应该可以工作。 :))

- (UIImage*)cropAndResizeAspectFillWithSize:(CGSize)targetSize 
         interpolationQuality:(CGInterpolationQuality)quality { 

    UIImage *outputImage = nil; 
    UIImage *imageForProcessing = self; 

    // crop center square (AspectFill) 
    if (self.size.width != self.size.height) { 
     CGFloat shorterLength = 0; 
     CGPoint origin = CGPointZero; 
     if (self.size.width > self.size.height) { 
      origin.x = (self.size.width - self.size.height)/2; 
      shorterLength = self.size.height; 
     } 
     else { 
      origin.y = (self.size.height - self.size.width)/2; 
      shorterLength = self.size.width; 
     } 
     imageForProcessing = [imageForProcessing normalizedImage]; 
     imageForProcessing = [imageForProcessing croppedImage:CGRectMake(origin.x, origin.y, shorterLength, shorterLength)]; 
    } 
    outputImage = [imageForProcessing resizedImage:targetSize interpolationQuality:quality]; 
    return outputImage; 
} 

// fix image orientation, which may wrongly rotate the output. 
- (UIImage *)normalizedImage { 
    if (self.imageOrientation == UIImageOrientationUp) return self; 

    UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); 
    [self drawInRect:(CGRect){0, 0, self.size}]; 
    UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return normalizedImage; 
} 
+0

这种方法如何帮助裁剪图像? – Dejell

+0

看起来,它调整了给定的图像,但不裁剪它 – Dejell

+0

你是对的。我意识到这一点,但忘了解决我的答案。将尽快做到;) – Hlung

相关问题