GKImagePicker
GKImagePicker copied to clipboard
Incorrect crop rect when move and scale squared images
GKImagePicker returns incorrectly cropped image for squared images (height == width). And I have found a solution, you need to replace in GKImageCropView.m this code:
" if (self.cropSize.width > self.cropSize.height) { scale = (self.imageToCrop.size.width < self.imageToCrop.size.height ? MAX(scaleWidth, scaleHeight) : MIN(scaleWidth, scaleHeight)); }else{ scale = (self.imageToCrop.size.width < self.imageToCrop.size.height ? MIN(scaleWidth, scaleHeight) : MAX(scaleWidth, scaleHeight)); }"
with this one ("<" replaced with "<="):
" if (self.cropSize.width > self.cropSize.height) { scale = (self.imageToCrop.size.width <= self.imageToCrop.size.height ? MAX(scaleWidth, scaleHeight) : MIN(scaleWidth, scaleHeight)); }else{ scale = (self.imageToCrop.size.width <= self.imageToCrop.size.height ? MIN(scaleWidth, scaleHeight) : MAX(scaleWidth, scaleHeight)); }"
sorry for my english :)
helped me a lot! thanks
Hmm still having issues on this one..
SjoerdPerfors, please ensure that your image is square(width==height), and if that is true, can you send me your image for testing?
Yeah I realized that too! Why is that?
What I do now (I only use the cropperViewController and have my Facebook photopicker first):
#import "UIImage+Scaling.h"
if(image.size.height > image.size.width) image = [image imageByScalingProportionallyToSize:CGSizeMake(image.size.height,image.size.height)]; else image = [image imageByScalingProportionallyToSize:CGSizeMake(image.size.width,image.size.width)];
to make it a square and it works. But I'm sure it's not the best solution.
I guess we are talking on different things. I use GKImagePicker for cropping images from camera and albums. And because of that GKImagePicker retuns incorrect crop rect when move and scale squared images, I decided to write here how to fix that problem. So, if your image heigth!=width initially (without transformations) or if you don't use GKImagePicker at all - I don't know how to help you, sorry.
Guess you're right! Thanks for your reply.
In GKImageCropView.m
Changed This... if (self.cropSize.width > self.cropSize.height) { scale = (self.imageToCrop.size.width < self.imageToCrop.size.height ? MAX(scaleWidth, scaleHeight) : MIN(scaleWidth, scaleHeight)); } else{ scale = (self.imageToCrop.size.width < self.imageToCrop.size.height ? MIN(scaleWidth, scaleHeight) : MAX(scaleWidth, scaleHeight)); }
To... if (self.cropSize.width > self.cropSize.height) { scale = (self.imageToCrop.size.width < self.imageToCrop.size.height ? MAX(scaleWidth, scaleHeight) : MIN(scaleWidth, scaleHeight)); } else if (self.cropSize.width == self.cropSize.height) { scale = MAX(scaleWidth, scaleHeight); } else{ scale = (self.imageToCrop.size.width < self.imageToCrop.size.height ? MIN(scaleWidth, scaleHeight) : MAX(scaleWidth, scaleHeight)); }
@aerialcombat BIG THANKS !! This helped me a lot !
aerialcombat - your post worked for me. I am going to update my "issue" from the board to indicate this is the fix.
Scale is now sometimes determined for the wrong axis because image scale should be based on image width and height, NOT crop width and height.
instead of this from the original function
if (self.cropSize.width > self.cropSize.height)
change it to this and you should be good to go.
if (self.imageToCrop.size.width > self.imageToCrop.size.height)
I had noticed the random wrong axis determination, but had not dug into it. I will put this in. Thanks for looking into it! Jonathan Date: Wed, 7 Aug 2013 01:43:51 -0700 From: [email protected] To: [email protected] CC: [email protected] Subject: Re: [GKImagePicker] Incorrect crop rect when move and scale squared images (#23)
Scale is now sometimes determined for the wrong axis because image scale should be based on image width and height, NOT crop width and height.
instead of this from the original function
if (self.cropSize.width > self.cropSize.height)
change it to this and you should be good to go.
if (self.imageToCrop.size.width > self.imageToCrop.size.height)
— Reply to this email directly or view it on GitHub.
@aerialcombat thank you :)
Just a heads up - there are a number of solutions on this thread. The post that solved my problem completely was the original poster's answer (@DvaChisla). The problem that it addressed was that when you selected a completely square image (height == width) from the library, the cropping would be completely off. I have a custom rectangle size as my crop and the final image I got originally was a square image that was completely different. The first posts's solution fixed it for me and now I achieve the result I was after. Cheers.
Above codes just work for either for Portrait or Landscape. But I needed to support both type of images. So, after several hours of googling I found following solution that works well.
-(CGRect)_calcVisibleRectForCropArea{
CGFloat scale = self.imageView.image.size.width / self.imageView.frame.size.width;
scale *= self.scrollView.zoomScale;
//extract visible rect from scrollview and scale it
CGRect visibleRect = [scrollView convertRect:scrollView.bounds toView:imageView];
visibleRect = GKScaleRect(visibleRect, scale);
return visibleRect;
}
Courtesy: https://github.com/fleshgolem/GKImagePicker/commit/89447bf11bed884824fff865927495ab4cb1f12e
Sweet. Thanks for this guys!
Thank you @aerialcombat your answer fix my error.