Ronak
Ronak

Reputation: 147

Cropping image from uiimagepickerviewcontroller in selected area

how to crop image from UIImagePickerController in selected area when capturing image. overlayview to UIImagePickerController

Upvotes: 2

Views: 3842

Answers (1)

user3182143
user3182143

Reputation: 9609

Below the Code

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

After that in imagePickerViewDelegate Method

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

Upvotes: 1

Related Questions