I have an App that takes a screenshot of a UIImageView with the following code:
-(IBAction) screenShot: (id) sender{
UIGraphicsBeginImageContext(sshot.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage,nil, nil, nil);
}
This works well but I need to be able to position where I take the screenshot basically I need to grad only a third of the screen (center portion). I tried using
UIGraphicsBeginImageContext(CGSize 150,150);
But have found that every thing is taken from 0,0 coordinates, has anyone any idea how to position this correctly.
Well the screenshot is taken from a canvas you draw.
So instead of drawing your layer in the whole context, with a reference to top left corner, you will draw it where you want to take the screenshot....
//first we will make an UIImage from your view
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//now we will position the image, X/Y away from top left corner to get the portion we want
UIGraphicsBeginImageContext(sshot.frame.size);
[sourceImage drawAtPoint:CGPointMake(-50, -100)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
From this
UIGraphicsBeginImageContext(sshot.frame.size);
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, 150, 150); // <-- shift everything up to required position when drawing.
[self.view.layer renderInContext:c];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
Use this method to crop if u have image with specfic rect to crop:
-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
Use like this:
UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)]; //example
If you like you can refer this code.
In this example you can get the image covered by the rect from any position and any zoom scale.
Happy Coding :)
Some extracted code for reference is as below
Main function or code used to crop the photo
- (UIImage *) croppedPhoto
{
CGFloat ox = self.scrollView.contentOffset.x;
CGFloat oy = self.scrollView.contentOffset.y;
CGFloat zoomScale = self.scrollView.zoomScale;
CGFloat cx = (ox + self.cropRectangleButton.frame.origin.x + 15.0f) * 2.0f / zoomScale;
CGFloat cy = (oy + self.cropRectangleButton.frame.origin.y + 15.0f) * 2.0f / zoomScale;
CGFloat cw = 300.0f / zoomScale;
CGFloat ch = 300.0f / zoomScale;
CGRect cropRect = CGRectMake(cx, cy, cw, ch);
NSLog(@"---------- cropRect: %@", NSStringFromCGRect(cropRect));
NSLog(@"--- self.photo.size: %@", NSStringFromCGSize(self.photo.size));
CGImageRef imageRef = CGImageCreateWithImageInRect([self.photo CGImage], cropRect);
UIImage *result = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
NSLog(@"------- result.size: %@", NSStringFromCGSize(result.size));
return result;
}
The details how to use the example is given here.
Enjoy Coding :)