0

Currently I am cropping the UIImage received from my UIImagePickerController with this method:

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
{
    double refWidth = CGImageGetWidth(image.CGImage);
    double refHeight = CGImageGetHeight(image.CGImage);

    double x = (refWidth - size.width) / 2.0;
    double y = (refHeight - size.height) / 2.0;

    CGRect cropRect = CGRectMake(x, y, size.height, size.width);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:image.imageOrientation];
    CGImageRelease(imageRef);

    return cropped;
}

I am trying to accurately, with complete precision, grab the square photo in the middle of the UIImagePickerControllerOriginalImage. To do so, I am using the method above in my method that triggers when the user has clicked "Use Photo" from the UIImagePickerController:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;

    picker.allowsEditing = YES;
    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);

    CGRect cropRect = CGRectMake(264, 0, 2448, 3000);
    CGSize sizeOfCrop = CGSizeMake(2550, 2500);  //<---NEED HELP, float sizes for photo of size 320x320(640x640)?

    UIImage *croppedImage = [self imageByCroppingImage:img toSize:sizeOfCrop]; //cropping the UIImage  

    UINavigationController *postControl = [self.storyboard instantiateViewControllerWithIdentifier:@"postControl"];
    RGPostViewController *postView = (RGPostViewController *)postControl.topViewController;

    [postView storeImage:imageToPass]; //store image in destinationViewController
    [self.presentedViewController presentViewController:postControl animated:NO completion:nil];
}

This code works correctly and does grab the center of the original image as I had hoped. The problem is that I do not know the correct CGFloat values to provide CGSize sizeOfCrop, the part where I added the comment "<--NEED HELP".

What CGFloat values must I pass to CGSizeMake in order to grab an exact perfect square picture from the middle? A picture that would perfectly fit an UIImageView with CGRect of width-height 320x320.

Chisx
  • 1,976
  • 4
  • 25
  • 53
  • I'd check out [UIImage-ResizeMagick](https://github.com/mustangostang/UIImage-ResizeMagick). It can do all of this for you so you don't have to worry about it. That said, simply `(long edge - short edge) / 2 = distance from Y coordinate 0`. – brandonscript Jul 09 '15 at 06:05
  • Okay is that for the height or width, and that's not simple. What are the edges? Is long edge the height and short edge the width. Also, I'm not trying to resize the photo. Im just cropping.. two different things – Chisx Jul 09 '15 at 19:19

1 Answers1

0

You can apply my comment to both height and width. Since you're dealing with a constant center point, you just need the delta between widths and heights, then divide by 2 to get the origin point. And since you already know the originating size and the target (crop) size, you can just draw the original image into the crop rect:

CGFloat cropWidth = 320;
CGFloat cropHeight = 320;

CGSize cropSize = CGSizeMake(cropWidth, cropHeight);
UIGraphicsBeginImageContext(cropSize);

CGFloat cropX = (originalImage.size.width / 2) - (cropWidth / 2);
CGFloat cropY = (originalImage.size.height / 2) - (cropHeight / 2);
CGSize cropRect = CGSizeMake(cropX, cropY, cropWidth, cropHeight);

[originalImage drawInRect:cropRect];

UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Caveat: This is untested. If it doesn't center properly, you should be able to use the principles in this answer to center the image.

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220