简体   繁体   English

iOS Custom UIImagePickerController Camera Crop to Square

[英]iOS Custom UIImagePickerController Camera Crop to Square

I'm trying to create a camera like Instagram where the user can see a box and the image would crop to that box. 我正在尝试创建一个像Instagram这样的相机,用户可以看到一个盒子,图像会裁剪到那个盒子。 For Some reason the camera doesn't go all the way to the bottom of the screen and cuts off near the end. 由于某些原因,相机不会一直到达屏幕底部并在末端附近切断。 I'm also wondering how would I go about cropping the image to be 320x320 exactly inside that square? 我也想知道如何在正方形内将图像裁剪为320x320?

在此输入图像描述

Here's the easiest way to do it (without reimplementing UIImagePickerController ). 这是最简单的方法(不重新实现UIImagePickerController )。 First, use an overlay to make the camera field look square. 首先,使用叠加使相机字段看起来是方形的。 Here's an example for 3.5" screens (you'd need to update it to work for iPhone 5): 以下是3.5英寸屏幕的示例(您需要将其更新为适用于iPhone 5):

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = source;

if (source == UIImagePickerControllerSourceTypeCamera) {
    //Create camera overlay
    CGRect f = imagePickerController.view.bounds;
    f.size.height -= imagePickerController.navigationBar.bounds.size.height;
    CGFloat barHeight = (f.size.height - f.size.width) / 2;
    UIGraphicsBeginImageContext(f.size);
    [[UIColor colorWithWhite:0 alpha:.5] set];
    UIRectFillUsingBlendMode(CGRectMake(0, 0, f.size.width, barHeight), kCGBlendModeNormal);
    UIRectFillUsingBlendMode(CGRectMake(0, f.size.height - barHeight, f.size.width, barHeight), kCGBlendModeNormal);
    UIImage *overlayImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView *overlayIV = [[UIImageView alloc] initWithFrame:f];
    overlayIV.image = overlayImage;
    [imagePickerController.cameraOverlayView addSubview:overlayIV];
}

imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];

Then, after you get a picture back from the UIImagePickerController , crop it to a square with something like this: 然后,在从UIImagePickerController获取图片后,将其裁剪为方形,如下所示:

//Crop the image to a square
CGSize imageSize = image.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
if (width != height) {
    CGFloat newDimension = MIN(width, height);
    CGFloat widthOffset = (width - newDimension) / 2;
    CGFloat heightOffset = (height - newDimension) / 2;
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), NO, 0.);
    [image drawAtPoint:CGPointMake(-widthOffset, -heightOffset)
                   blendMode:kCGBlendModeCopy
                       alpha:1.];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

And you're done. 而且你已经完成了。

@Anders answer was very close to correct for me on iPhone 5. I made the following modification to add an overlay hardcoded for iPhone 5: @Anders在iPhone 5上的回答非常接近正确。我做了以下修改,为iPhone 5添加了一个硬编码的覆盖:

CGRect f = imagePickerController.view.bounds;
f.size.height -= imagePickerController.navigationBar.bounds.size.height;
UIGraphicsBeginImageContext(f.size);
[[UIColor colorWithWhite:0 alpha:.5] set];
UIRectFillUsingBlendMode(CGRectMake(0, 0, f.size.width, 124.0), kCGBlendModeNormal);
UIRectFillUsingBlendMode(CGRectMake(0, 444, f.size.width, 52), kCGBlendModeNormal);
UIImage *overlayImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *overlayIV = [[UIImageView alloc] initWithFrame:f];
overlayIV.image = overlayImage;
overlayIV.alpha = 0.7f;
[imagePickerController setCameraOverlayView:overlayIV];`

I hope this helps someone. 我希望这可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM