简体   繁体   English

使用相机拍摄的iOS裁剪图像

[英]iOS cropping image taken with camera

I am using the camera ( iOS 7 , iPad mini ) to take a picture and crop it to a square. 我正在使用相机( iOS 7iPad mini )拍摄照片并将其裁剪为正方形。 I am pretty content with the default cropping functionality I get from the following code: 我很满意从以下代码中获得的默认裁剪功能:

    imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePickerController.allowsEditing = YES;
    UIImage *image = info[UIImagePickerControllerEditedImage]

It gives me a squared overlay over the image I took. 它使我在拍摄的图像上覆盖了一个正方形。 If I zoom, I can pan the image a little. 如果缩放,则可以稍微平移图像。 However I am surprised I can not move the image up and down (without zoom) to move the crop rectangle over the Image. 但是,令我惊讶的是我无法上下移动图像(不缩放)以在图像上移动裁剪矩形。 If I try to do so, it just bounces back to the original location. 如果我尝试这样做,它只会弹回到原始位置。 What am I missing? 我想念什么? Or is this functionality just missing? 还是只是缺少此功能?

From the many questions here, most are answered with a disappointing 'It has to be done in a custom way'. 从这里的许多问题中,大多数问题令人失望地回答:“必须以自定义方式完成”。

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

This is Objective c Code. 这是Objective c代码。

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

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