我正在使用UIImagePickerController并自定义" cameraOverlay"。
- (UIImagePickerController *)imagePickerCtrl
{
if (_imagePickerCtrl)
{
return _imagePickerCtrl;
}
_imagePickerCtrl = [[UIImagePickerController alloc] init];
_imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePickerCtrl.showsCameraControls = NO;
_imagePickerCtrl.delegate = self;
CameraOverlayView *overlay = [[CameraOverlayView alloc] initWithFrame:(CGRect){0, 0, SCREENWIDTH, SCREENHEIGHT} innerRectSize:CGSizeMake(SCREENWIDTH - 10 * 2, (SCREENWIDTH - 10 * 2) * 0.618) innerCornerRadius:20.0];
_imagePickerCtrl.cameraOverlayView = overlay;
@WeakObj(self);
[overlay setBlock:^(NSInteger tag) {
if (!tag)//取消
{
[weakself.imagePickerCtrl dismissViewControllerAnimated:YES completion:nil];
}
else//照相
{
[weakself.imagePickerCtrl takePicture];
}
}];
return _imagePickerCtrl;
}
以下是截图:
我的问题是我不知道如何获得裁剪图像的正确框架。() 这是我用于裁剪图像的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
CameraOverlayView *overlay = picker.cameraOverlayView;
// NSLog(@"%@", NSStringFromCGRect(overlay.cropFrame));
UIImage *croppedImage = [self cropImage:[image fixOrientation] cropFrame:overlay.cropFrame];// the fixOrientation is a category from someone else.
if (_block)
{
_block(croppedImage);
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (UIImage *)cropImage:(UIImage *)originalImage cropFrame:(CGRect)cropFrame
{
CGImageRef resultImageRef = CGImageCreateWithImageInRect(originalImage.CGImage, cropFrame);//I m pretty sure the key is got the The crop frame.
UIImage *newImage = [[UIImage alloc] initWithCGImage:resultImageRef scale:1.0 orientation:originalImage.imageOrientation];
return newImage;
}