12

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.

yeha
  • 149
  • 1
  • 1
  • 7

4 Answers4

30

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);
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • 2
    What is `sshot.frame.size`? Did you mean `self.view.frame.size`? – progrmr Apr 17 '14 at 17:06
  • It's the size of the screenshot. You could change that line to `UIGraphicsBeginImageContext(CGSizeMake(200,200));' to make a 200X200 screenshot image – Lefteris Mar 09 '16 at 19:27
14

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);
Community
  • 1
  • 1
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139
7

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
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

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

The iOSDev
  • 5,237
  • 7
  • 41
  • 78