简体   繁体   English

根据UIView / CGRect的大小将图像裁剪为正方形

[英]Crop image to a square according to the size of a UIView/CGRect

I have an implementation of AVCaptureSession and my goal is for the user to take a photo and only save the part of the image within the red square border, as shown below: 我有一个AVCaptureSession的实现,我的目标是让用户拍照并只保存红色方框边框内的图像部分,如下所示:

UIViewController与AVCaptureSession的实现

AVCaptureSession's previewLayer (the camera) spans from (0,0) (top left) to the bottom of my camera controls bar (the bar just above the view that contains the shutter). AVCaptureSession的previewLayer (相机)从(0,0) (左上角)跨越到相机控制条的底部(包含快门的视图上方的条形图)。 My navigation bar and controls bar are semi-transparent, so the camera can show through. 我的导航栏和控制栏是半透明的,因此相机可以显示。

I'm using [captureSession setSessionPreset:AVCaptureSessionPresetPhoto]; 我正在使用[captureSession setSessionPreset:AVCaptureSessionPresetPhoto]; to ensure that the original image being saved to the camera roll is like Apple's camera. 确保保存到相机胶卷的原始图像就像Apple的相机一样。

The user will be able to take the photo in portrait, landscape left and right, so the cropping method must take this into account. 用户将能够以纵向,左右横向拍摄照片,因此裁剪方法必须考虑到这一点。

So far, I've tried to crop the original image using this code: 到目前为止,我已尝试使用此代码裁剪原始图像:

DDLogVerbose(@"%@: Image crop rect: (%f, %f, %f, %f)", THIS_FILE, self.imageCropRect.origin.x, self.imageCropRect.origin.y, self.imageCropRect.size.width, self.imageCropRect.size.height);

// Create new image context (retina safe)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.imageCropRect.size.width, self.imageCropRect.size.width), NO, 0.0);

// Create rect for image
CGRect rect = self.imageCropRect;

// Draw the image into the rect
[self.captureManager.stillImage drawInRect:rect];

// Saving the image, ending image context
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();

However, when I look at the cropped image in the camera roll, it seems that it has just squashed the original image, and not discarded the top and bottom parts of the image like I'd like. 然而,当我看到相机胶卷中的裁剪图像时,似乎它刚刚压缩了原始图像,而不是像我想的那样丢弃图像的顶部和底部。 It also results in 53 pixels of white space at the top of the "cropped" image, likely because of the y position of my CGRect . 它还会在“裁剪”图像的顶部产生53像素的空白区域,这可能是因为我的CGRect的y位置。

This is my logging output for the CGRect : 这是我对CGRect日志输出:

 Image crop rect: (0.000000, 53.000000, 320.000000, 322.000000)

This also describes the frame of the red bordered view in the superview. 这也描述了superview中红色边框视图的框架。

Is there something crucial I'm overlooking? 我有什么关键吗?

PS The original image size (taken with a camera in portrait mode) is: PS原始图像尺寸(在纵向模式下使用相机拍摄)是:

Original image size: (2448.000000, 3264.000000)

You can crop images with CGImageCreateWithImageInRect : 您可以使用CGImageCreateWithImageInRect裁剪图像:

CGImageRef imageRef = CGImageCreateWithImageInRect([uncroppedImage CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

Don't forget to add scale parameter otherwise you will get low resolution image 不要忘记添加比例参数,否则您将获得低分辨率图像

CGImageRef imageRef = CGImageCreateWithImageInRect([uncroppedImage CGImage], CGRectMake(0, 0, 30, 120));
[imageView setImage:[UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp]];
CGImageRelease(imageRef);

Swift 3: 斯威夫特3:

let imageRef:CGImage = uncroppedImage.cgImage!.cropping(to: bounds)!
let croppedImage:UIImage = UIImage(cgImage: imageRef)

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

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