Hei, I went through the example for saving images and afterwards I wanted to save only a part of the screen. I managed to save the part starting at the upper left corner of the image but I actually want to save the center of my screen. The magic to save only a part of an image is setting up the Graphics Context with a certain size, like this:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), YES, 5.0f);
I thought there might be a way to use a CGRect instead of the size, but that gives me an error. any other attempts or thoughts? Do I have to go through the pixels of my screenshot, grab those needed and make a new image out of that (that would be the kind of complicated way I can think of but maybe there is an easier one)?
To do this with C4Image objects, you can modify incmiko's answer to look like the following:
Aside from the comments in the code there are a couple other things to be aware of:
The "area" you're cropping will always be in reference to the "image" that you're cropping. So, if you want to crop
{150,50}
from the image, and the image's origin is at{20,20}
then it will LOOK like you are cropping{170,70}
from the CANVAS.The
C4Image
object actually has arenderInContext:
method, so you don't have to do this from the image's layer.C4Image
objects wrapUIImage
objects, which is why we build a new one using theUIImage
that we get from the current contexta little modification to Travis' answer makes it independent from the image but dependent on the canvas origin:
This method what I wrote for this is works perfectly:
for example, if you want to make a printscreen of your main view, in (100,50,100,100)