Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect crop rect when move and scale squared images #23

Open
TkachenkoViacheslav opened this issue May 30, 2013 · 16 comments
Open

Incorrect crop rect when move and scale squared images #23

TkachenkoViacheslav opened this issue May 30, 2013 · 16 comments

Comments

@TkachenkoViacheslav
Copy link

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 :)

@jbouaziz
Copy link

jbouaziz commented Jun 7, 2013

helped me a lot! thanks

@SjoerdPerfors
Copy link

Hmm still having issues on this one..

@TkachenkoViacheslav
Copy link
Author

SjoerdPerfors, please ensure that your image is square(width==height), and if that is true, can you send me your image for testing?

@SjoerdPerfors
Copy link

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.

@TkachenkoViacheslav
Copy link
Author

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.

@SjoerdPerfors
Copy link

Guess you're right! Thanks for your reply.

@aerialcombat
Copy link

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));
}

@SjoerdPerfors
Copy link

@aerialcombat BIG THANKS !! This helped me a lot !

@kuga0509
Copy link

aerialcombat - your post worked for me. I am going to update my "issue" from the board to indicate this is the fix.

@joshbernfeld
Copy link

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)

@kuga0509
Copy link

kuga0509 commented Aug 8, 2013

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.

@jalola
Copy link

jalola commented Jun 16, 2014

@aerialcombat thank you :)

@micnguyen
Copy link

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.

@maksumon
Copy link

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: fleshgolem@89447bf

@Richallen1
Copy link

Sweet. Thanks for this guys!

@origds
Copy link

origds commented Jan 29, 2019

Thank you @aerialcombat your answer fix my error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests