0

I am using the following crop method to crop a uiimage that's sitting in a UIImageView which is then sitting in a UIScrollView.

-(UIImage *)cropImage:(UIImage *)image
{
    float scale = 1.0f/_scrollView.zoomScale;

    NSLog(@"Oh and heres that zoomScale: %f", _scrollView.zoomScale);

    CGRect visibleRect;
    visibleRect.origin.x = _scrollView.contentOffset.x * scale;
    visibleRect.origin.y = _scrollView.contentOffset.y * scale;
    visibleRect.size.width = _scrollView.bounds.size.width * scale;
    visibleRect.size.height = _scrollView.bounds.size.height * scale;

    NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.x);
    NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.y);
    NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.width);
    NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.height);

    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], visibleRect);
    UIImage *croppedImage = [[UIImage alloc] initWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return croppedImage;
}

I need the image to be cropped to a CGSize of (321,115). Upon cropping the image and seeing the print results, I can see that visibleRect is (0,0,321,115) - what it is supposed to be, the croppedImage UIImage then has width:321 and height:115. for some reason however the image appears to be zoomed in entirely too far (the method cropped a smaller portion of the original image to a size of 321x115).

Why is this method not correctly cropping my image?

-As a side note: When I call this method, I am calling like so _croppedImage = [self cropImage:_imageView.image]; which sets a UIImage property of a custom UIView class to the cropped image.

Chisx
  • 1,976
  • 4
  • 25
  • 53

1 Answers1

1

Please try this function. It may help you.

Parameters:

  1. UIImage
  2. CGSize (321,115) or any size

// crop image - image will crop from full image

- (UIImage *)cropImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    double ratio;
    double delta;
    CGPoint offset;

    //make a new square size, that is the resized imaged width
    CGSize sz = CGSizeMake(newSize.width, newSize.width);

    //figure out if the picture is landscape or portrait, then
    //calculate scale factor and offset
    if (image.size.width > image.size.height) {
        ratio = newSize.width / image.size.width;
        delta = (ratio*image.size.width - ratio*image.size.height);
        offset = CGPointMake(delta/2, 0);
    }
    else {
        ratio = newSize.width / image.size.height;
        delta = (ratio*image.size.height - ratio*image.size.width);
        offset = CGPointMake(0, delta/2);
    }

    //make the final clipping rect based on the calculated values
    CGRect clipRect = CGRectMake(-offset.x,
                                 -offset.y,
                                 (ratio * image.size.width) + delta,
                                 (ratio * image.size.height) + delta);


    //start a new context, with scale factor 0.0 so retina displays get
    //high quality image
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
    } else {
        UIGraphicsBeginImageContext(sz);
    }
    UIRectClip(clipRect);
    [image drawInRect:clipRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

To crop only selected portion of image

Please check this link

Eiko
  • 25,601
  • 15
  • 56
  • 71
iDeveloper
  • 607
  • 5
  • 25
  • how does this code take into account my UIScrollView zoomScale? I have to take into account the scaleFactor of the UIScrollView, otherwise this isnt cropping the selected area – Chisx May 30 '16 at 04:25
  • It means you want to crop only selected area. right ? If yes than please check edited answer – iDeveloper May 30 '16 at 04:37
  • Yes I have it almost working using http://stackoverflow.com/questions/7760191/cropping-a-uiimage-according-to-the-viewport-inside-a-uiscrollview but there are issues with that code.. only slightly off. And yes.. only selected area.. Basically the zoomed area or the area that appears in the UIScrollView. That is close, but that's not quite what Im looking for either, the link I shared here is basically what I was looking for but it does not fully work corerctly. – Chisx May 30 '16 at 04:41