Hari Mohan
Hari Mohan

Reputation: 224

Image Cropping Issue for Cropping an Image

I want to crop an image, and I have searched many libraries but have not found a perfect answer.

I want to crop an image using rectangle shape, which will have a fixed maximum height and width (300, 200), and a fixed minimum height and width (1.0, 1.0). The rectangle will also be movable, and can be resized to any size within the maximum and minimum dimensions.

Upvotes: 2

Views: 1099

Answers (2)

Amit Verma
Amit Verma

Reputation: 1413

You can use TimOliver/TOCropViewController and change your cropping frame according to demand from TOCropViewController.m class,

1: Download full project from https://github.com/TimOliver/TOCropViewController.

2:Drag and drop TOCropViewController.h,TOCropViewController.m and class with other classes.

3:set ratio according to demand in TOCropViewController.m class with method [self setAspectRatioPreset:self.aspectRatioPreset animated:NO];

Upvotes: 1

Rohit Kashyap
Rohit Kashyap

Reputation: 934

I only understand your question partially, if you are looking to crop an UIImage, check out the below code:

//Pass the UIImage object and the varying rectangle you "outputrect"
     - (UIImage *)cropImage:(UIImage *)image outPutRect:(CGRect) outputRect
    {

         CGImageRef takenCGImage = image.CGImage;
        size_t width = CGImageGetWidth(takenCGImage);
        size_t height = CGImageGetHeight(takenCGImage);
        CGRect cropRect = CGRectMake(outputRect.origin.x * width, outputRect.origin.y * height,
                                     outputRect.size.width * width, outputRect.size.height * height);

        CGImageRef cropCGImage = CGImageCreateWithImageInRect(takenCGImage, cropRect);
        image = [UIImage imageWithCGImage:cropCGImage scale:1 orientation:image.imageOrientation];
        CGImageRelease(cropCGImage);

        return image;
    }

Upvotes: 3

Related Questions