I am trying to use CGImageCreateWithImageInRect to get a sub-image from an image but it's not working properly. It returns the image from a random place in the image, rather than the one I specify. Please take a look at the following image;
My code is:
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(524, 1331, 600, 600));
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
But that doesn't work. What am I doing wrong?
Thanks!
It is likely that your image has a UIImageOrientation set for it that is different from what you are expecting. The iOS will display the image properly, yet the image will be stored sideways (for lack of a better term).
So when you say give me the rect starting at x, y you will get y, x instead, or some random craziness because you are accessing a rect that is outside of the normal range.
This answer talks about fixing the orientation.
please see followed answer, you need transform the rect or transform the image. I prefer to transforming the rect.
https://stackoverflow.com/a/21560825/2482283
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With