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.