iOS:使用叠加层裁剪从UIImagePickerController摄像头抓取的静止图像 [英] iOS: Cropping a still image grabbed from a UIImagePickerController camera with overlay

查看:113
本文介绍了iOS:使用叠加层裁剪从UIImagePickerController摄像头抓取的静止图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS的新手,过去一周我在网上寻找这样的教程,例如:处理Exif图像调整图像大小,以及StackOverflow上的更多随机问题。从这些,我认为> = iOS 4.0 ,从相机拍摄的所有图像都包含基于 EXIF 的旋转信息。

I am new to iOS and I have been bashing head against the wall for this for the past week looking around online for tutorials such as: Dealing with Exif Images, Resizing images, and many more random questions here on StackOverflow. From these, I figured that >=iOS 4.0, all images taken from the camera contain EXIF-based rotation information.

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

What's not working: After trying different image cropping techniques, all I end up with is cropping of the image at some random corners and also, the resulting image appears to be zoomed in :( When I use a png image from the internet (which don't contain EXIF data), the cropping is working. By random corners, I mean - the image ends up being cropped at the top-right/top-left and zoomed-in.

我想要实现的目标:

我正在尝试从顶部裁剪图像 100 px 并且 100 px 从底部开始。基本上,我使用两个叠加条 - 顶部为1,底部为1, CGRect(0.0,0.0,SCREEN_WIDTH,100.0) [a 100.0 px 顶部的高条带]和另一个 CGRect(0.0,SCREEN_HEIGHT - 100,SCREEN _WIDTH,100.0) [另一个 100 px 底部的高条]。我需要在这两个条带之间获取图像:我假设图像的高度为: SCREEN_HEIGHT - 200.0

What I am trying to accomplish:
I am trying to crop an image 100 px from top and 100 px from bottom. Essentially, I am using two overlay strips - 1 at the top, and 1 at the bottom with the CGRect(0.0, 0.0, SCREEN_WIDTH, 100.0) [a 100.0 px tall strip at the top] and another CGRect(0.0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100.0) [another 100 px tall strip at the bottom]. I need to get the image between these two strips: I assume the height of the image is: SCREEN_HEIGHT - 200.0.

显示覆盖相机的UIImagePickerController:



    //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];

基于EXIF旋转图像的代码 imageOrientation 属性并裁剪

Code to rotate image based on EXIF imageOrientation property and cropping it



- (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;
    }


cropImage 方法似乎适用于从互联网下载的图像(不包含任何方向信息)。
我的选项用完了。有人可以帮帮我吗?

The cropImage method seems to work for images downloaded from the internet (which don't contain any orientation info). I am running out of options. Could someone PLEASE help me out?

感谢阅读!

推荐答案

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

When you can, it is easier to skip drawing images with 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;
}

这篇关于iOS:使用叠加层裁剪从UIImagePickerController摄像头抓取的静止图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆