GKImagePicker icon indicating copy to clipboard operation
GKImagePicker copied to clipboard

Incorrect crop rect when move and scale squared images

Open TkachenkoViacheslav opened this issue 11 years ago • 16 comments

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

TkachenkoViacheslav avatar May 30 '13 08:05 TkachenkoViacheslav

helped me a lot! thanks

jbouaziz avatar Jun 07 '13 00:06 jbouaziz

Hmm still having issues on this one..

SjoerdPerfors avatar Jun 21 '13 08:06 SjoerdPerfors

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

TkachenkoViacheslav avatar Jun 21 '13 19:06 TkachenkoViacheslav

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.

SjoerdPerfors avatar Jun 22 '13 09:06 SjoerdPerfors

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.

TkachenkoViacheslav avatar Jun 22 '13 10:06 TkachenkoViacheslav

Guess you're right! Thanks for your reply.

SjoerdPerfors avatar Jun 24 '13 07:06 SjoerdPerfors

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 avatar Jul 03 '13 11:07 aerialcombat

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

SjoerdPerfors avatar Jul 15 '13 12:07 SjoerdPerfors

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

kuga0509 avatar Jul 22 '13 19:07 kuga0509

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)

joshbernfeld avatar Aug 07 '13 08:08 joshbernfeld

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.

kuga0509 avatar Aug 08 '13 13:08 kuga0509

@aerialcombat thank you :)

jalola avatar Jun 16 '14 15:06 jalola

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.

micnguyen avatar Oct 01 '14 01:10 micnguyen

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

maksumon avatar Feb 22 '16 08:02 maksumon

Sweet. Thanks for this guys!

Richallen1 avatar Jun 30 '18 21:06 Richallen1

Thank you @aerialcombat your answer fix my error.

origds avatar Jan 29 '19 08:01 origds