私は 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;
}