I m using UIImagePickerController and customize the "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;
}
Here is the screen shot:
My problem is I don't know how to get the correct frame of the cropped image.() Here is the code I used to crop image:
- (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;
}