iOS:使用叠加层裁剪从UIImagePickerController摄像头抓取的静止图像

时间:2011-12-13 19:11:44

标签: iphone ios image-processing crop

我是iOS的新手,过去一周我在网上寻找教训,例如:Dealing with Exif ImagesResizing images以及更多随机问题堆栈溢出。根据这些,我认为>=iOS 4.0,从相机拍摄的所有图像都包含基于EXIF的旋转信息。

什么不起作用:在尝试了不同的图像裁剪技术后,我最终得到的就是在一些随机角落裁剪图像,而且,生成的图像似乎放大了:(当我使用来自互联网的png图像(不包含EXIF数据),裁剪工作正常。通过随机角落,我的意思是 - 图像最终被裁剪在右上角/左上角并进行缩放-in。

我想要完成的任务:
我正在尝试从顶部裁剪图像100 px,从底部裁剪100 px。基本上,我使用两个叠加条 - 顶部为1个,底部为1个,顶部有CGRect(0.0, 0.0, SCREEN_WIDTH, 100.0) [一个100.0 px高条带],另一个CGRect(0.0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100.0) [另一个{{ 1}}在底部的高条]。我需要在这两个条带之间获取图像:我假设图像的高度为: 100 px

显示覆盖相机的UIImagePickerController:

SCREEN_HEIGHT - 200.0

基于EXIF //SCREEN_HEIGHT = 480 and SCREEN_WIDTH = 320 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) { NSLog(@"Camera not available"); return; } imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.delegate = self; imagePicker.allowsEditing = NO; // Hide the controls imagePicker.showsCameraControls = NO; imagePicker.navigationBarHidden = YES; // Make camera view full screen imagePicker.wantsFullScreenLayout = YES; //imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y); //Overlay //OverlayView is a plain UIView with the CGRects mentioned in the question. OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; [overlay initOverlay:self]; imagePicker.cameraOverlayView = overlay; [self presentModalViewController:imagePicker animated:YES]; 属性旋转图片并裁剪的代码

imageOrientation

- (UIImage *) cropImage: (UIImage *) originalImage { CGRect cropRect = CGRectMake(0, 100.0, SCREEN_WIDTH, SCREEN_HEIGHT - 100); CGRect transformedRect = [self TransformCGRectForUIImageOrientation:cropRect :originalImage.imageOrientation :originalImage.size]; CGImageRef resultImageRef = CGImageCreateWithImageInRect(originalImage.CGImage, transformedRect); UIImage *newImage = [[[UIImage alloc] initWithCGImage:resultImageRef scale:1.0 orientation:originalImage.imageOrientation] autorelease]; return newImage; } - (CGRect) TransformCGRectForUIImageOrientation: (CGRect) source: (UIImageOrientation) orientation: (CGSize) imageSize { switch (orientation) { case UIImageOrientationLeft: { // EXIF #8 CGAffineTransform txTranslate = CGAffineTransformMakeTranslation( imageSize.height, 0.0); CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate, M_PI_2); return CGRectApplyAffineTransform(source, txCompound); } case UIImageOrientationDown: { // EXIF #3 CGAffineTransform txTranslate = CGAffineTransformMakeTranslation( imageSize.width, imageSize.height); CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate, M_PI); return CGRectApplyAffineTransform(source, txCompound); } case UIImageOrientationRight: { // EXIF #6 CGAffineTransform txTranslate = CGAffineTransformMakeTranslation( 0.0, imageSize.width); CGAffineTransform txCompound = CGAffineTransformRotate(txTranslate, M_PI + M_PI_2); return CGRectApplyAffineTransform(source, txCompound); } case UIImageOrientationUp: // EXIF #1 - do nothing default: // EXIF 2,4,5,7 - ignore return source; } 方法似乎适用于从互联网下载的图像(不包含任何方向信息)。 我的选项已经用完了。有人可以帮帮我吗?

感谢阅读!

1 个答案:

答案 0 :(得分:17)

如果可以,使用Core Graphics跳过图像会更容易:

- (UIImage *)cropImage:(UIImage *)oldImage {
    CGSize imageSize = oldImage.size
    UIGraphicsBeginImageContextWithOptions( CGSizeMake( imageSize.width,
                                                        imageSize.height - 200),
                                            NO,
                                            0.);
    [oldImage drawAtPoint:CGPointMake( 0, -100)
                blendMode:kCGBlendModeCopy
                    alpha:1.];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return croppedImage;
}