捕获图像时如何从选定区域中的UIImagePickerController裁剪图像。 overlayview到UIImagePickerController

最佳答案

代码下方

-(UIImage *)centerCropImage:(UIImage *)image
{
   // Use smallest side length as crop square length
   CGFloat squareLength = MIN(image.size.width, image.size.height);
   // Center the crop area
   CGRect clippedRect = CGRectMake((image.size.width - squareLength) / 2, (image.size.height - squareLength) / 2, squareLength, squareLength);
   // Crop logic
   CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
   UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
   CGImageRelease(imageRef);
   return croppedImage;
}

之后,在imagePickerViewDelegate方法中
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey: @"UIImagePickerControllerOriginalImage"];
    [self centerCropImage:image]; //Just give your image here for cropping
    yourImage.image=image;
    picker.delegate =self;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

10-05 18:28