Anupama
Anupama

Reputation: 373

How to crop the image in objective c?

The user can change the cropbox size which is shows default in edit screen. I tried with below code :

- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect {

    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef); 
    return cropped;
}

But it cropped fixed area. How to crop area which is selected by user ?

Upvotes: 10

Views: 18389

Answers (2)

iPatel
iPatel

Reputation: 47049

For Get Crop Image:

UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake("AS YOu Need"); //set your rect size.
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

Use following code for call croppIngimageByImageName:toRect: method that return UIImage (with specific size of image)

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }

Upvotes: 29

Mohit
Mohit

Reputation: 359

    CGRect clippedRect  = CGRectMake(0 ,0,180 ,180);
    CGImageRef imageRef = CGImageCreateWithImageInRect(imgVw1.image.CGImage, clippedRect);
    UIImage *newImage   = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    imgVw1Cliped.image=newImage;

    NSLog(@"%d",imgVw1Cliped.image.imageOrientation);

Upvotes: 8

Related Questions