Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 556
Anupama Avatar asked Aug 13 '13 09:08

Anupama


People also ask

How do you crop Ciimage?

To crop an image, make an image graphics context of the desired cropped size and call draw(at:) on the UIImage to draw it at the desired point relative to the graphics context, so that the desired portion of the image falls into the context. Now extract the resulting new image and close the context.

How do I crop in UIImage Swift?

size let xOffset = (sourceSize. width - sideLength) / 2.0 let yOffset = (sourceSize. height - sideLength) / 2.0 // The cropRect is the rect of the image to keep, // in this case centered let cropRect = CGRect( x: xOffset, y: yOffset, width: sideLength, height: sideLength ).

How do you cut an image in Python?

In Python, you crop the image using the same method as NumPy array slicing. To slice an array, you need to specify the start and end index of the first as well as the second dimension. The first dimension is always the number of rows or the height of the image.

What is UIImage?

An object that manages image data in your app.


2 Answers

    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);
like image 108
Mohit Avatar answered Oct 08 '22 03:10

Mohit


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;
    }
like image 43
iPatel Avatar answered Oct 08 '22 03:10

iPatel