Hi, I have a path (shape) and a high-resolution image. I make the high res image to be AspectFit inside the view on which I draw the path and I want to mask the image with the path but at the full resolution of the image, not at the resolution in which we see the path. The problem, It works perfectly when I don't upscale them up for high-resolution masking but when I do, everything is messed up. The mask gets stretched and the origins don't make sense.
All I want is to be able to upscale the path with the same aspect ratio of the image (at the full resolution of the image) and position it correctly so It can mask the high res image properly. I've tried this:
Masking CGContext with a CGPathRef?
and this
Creating mask with CGImageMaskCreate is all black (iphone)
and this
Clip UIImage to UIBezierPath (not masking)
none of which works correctly when I try to mask a high quality image (bigger than screen resolution)
EDIT I posted a working project to show the problem between normal quality masking (at screen's resolution) and high quality masking (at image's resolution) on github. I'd really appreciate any help. https://github.com/Reza-Abdolahi/HighResMasking
As the secondary answer, I made it work with this code and for better understanding, you can get the working project on github here as well to see if it works on all cases or not. my github project : https://github.com/Reza-Abdolahi/HighResMasking
The part of code that solved the problem:
If I understand your question correctly:
UIViewContentModeScaleAspectFit
.And now you want to create a copy of the image, at its original resolution, masked by the bezier path.
We can think of the image as having its own geometry, with the origin at the top left corner of the image and one unit along each axis being one point. So what we need to do is:
Step 2 is the hard one, because we have to come up with the correct
CGAffineTransform
. In an aspect-fit scenario, the transform needs to not only scale the image, but possibly translate it along either the x axis or the y axis (but not both). But let's be more general and support otherUIViewContentMode
settings. Here's a category that lets you ask aUIImageView
for the transform that converts points in the view's geometry to points in the image's geometry:Armed with this, we can write the code that masks the image. In my test app, I have a subclass of
UIImageView
namedPathEditingView
that handles the bezier path editing. So my view controller creates the masked image like this:And it looks like this:
Of course it's hard to tell that the output image is full-resolution. Let's fix that by cropping the output image to the bounding box of the bezier path:
Masking and cropping together look like this:
You can see in this demo that the output image has much more detail than was visible in the input view, because it was generated at the full resolution of the input image.