从选定区域的uiimagepickerviewcontroller裁剪图像

时间:2014-10-29 10:37:22

标签: ios objective-c uiimagepickercontroller

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

1 个答案:

答案 0 :(得分:1)

代码

-(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];
}