2

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.

kbunarjo
  • 1,277
  • 2
  • 11
  • 27
Hari Mohan
  • 214
  • 1
  • 4
  • 16
  • Are you trying to find an app for this task, or are you trying to create an app to do this? If you're trying to create an app, what language are you using, and what have you already tried? – kbunarjo Jan 07 '17 at 08:09
  • I have use Objective-c.. – Hari Mohan Jan 07 '17 at 08:10
  • I'm not too sure how much of your code you've already written, but does this help? http://stackoverflow.com/questions/18205672/how-to-crop-the-image-in-objective-c – kbunarjo Jan 07 '17 at 08:17
  • 1
    Possible duplicate of [Image Cropping API for iOS](http://stackoverflow.com/questions/7087435/image-cropping-api-for-ios) – kbunarjo Jan 07 '17 at 08:20
  • Actually i don't written any code, but i want to this in objective-c. i have not perfect library... – Hari Mohan Jan 07 '17 at 08:22
  • Can you provide any library, or coding part, – Hari Mohan Jan 07 '17 at 08:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132554/discussion-between-hari-mohan-and-kbunarjo). – Hari Mohan Jan 07 '17 at 09:07

2 Answers2

3

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;
    }
Rohit Kashyap
  • 934
  • 2
  • 8
  • 23
  • I want to crop an image dynamically in rectangle shape, and rectangle have a fixed maximum height and width (300, 200), and a fixed minimum height and width (1.0, 1.0). rectangle will also be movable, and can be resized to any size within the maximum and minimum dimensions. – Hari Mohan Jan 07 '17 at 09:35
  • You could pass your movable rect as "outputrect" in the above method and it will return you the UIImage cropped to that output rect. Please up vote. Cheers. – Rohit Kashyap Jan 07 '17 at 09:54
1

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

Amit Verma
  • 1,393
  • 1
  • 8
  • 7
  • This is long process but perfect for cropping. – Amit Verma Jan 07 '17 at 08:53
  • Can you provide the coding part, for rectangle which will have a fixed maximum height and width (300, 200), and a fixed minimum height and width (1.0, 1.0) and can be movable. – Hari Mohan Jan 07 '17 at 08:54
  • You can set ratio 3:2 on - (void)setAspectRatioPreset:(TOCropViewControllerAspectRatioPreset)aspectRatioPreset animated:(BOOL)animated switch (aspectRatioPreset) { case TOCropViewControllerAspectRatioPresetOriginal: // aspectRatio = CGSizeZero; aspectRatio = CGSizeMake(3.0f, 2.0f);} and it can be movable on this ratio . – Amit Verma Jan 07 '17 at 09:44