iOS 7 taking screenshot of part of a UIView - ios

I have a view (testView) that is 400x320 and I need to take a screenshot of part of this view (say rect = (50, 50, 200, 200)).
I am playing around with the drawViewHierarchy method in iOS 7 but I can't figure out how to do it correctly.
UIGraphicsBeginImageContextWithOptions(self.testView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.testView drawViewHierarchyInRect:self.testView.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Any help will be appreciated!
Thanks.

After getting the whole snapshot, you could draw it in a smaller Graphic Context in a way that you get the part you want:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), YES, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(-50, -50, image.size.width, image.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
EDIT: Better yet, draw the hierarchy directly in that smaller context:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, [UIScreen mainScreen].scale);
[self.testView drawViewHierarchyInRect:CGRectMake(-50, -50, self.testView.bounds.size.width, self.testView.bounds.size.height) afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Try below line of code,
UIView *popSnapshot=[inputView snapshotViewAfterScreenUpdates:YES];

Related

Capture a part of screen takes time

I am using this code to capture the part of iPad screen.
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect = CGRectMake(500, 500, 600, 600);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage],
rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
CGImageRelease(imageRef);
But this is very slow in comparison to capture part of screen starting from {0,0} to {100,100} using the code
CGRect rect = CGRectMake(0, 0, 200, 200);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[yourView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The difference between them is the rect size passes to UIGraphicsBeginImageContext. Can we capture CGRectMake(500, 500, 600, 600) without passing complete screen bounds to GraphicContext?
Please write code too.
Sure, you can capture just a smaller part of the view. Create the context at size 600x600, then translate the origin of the context to 500,500 before asking the layer to render.
CGRect rect = CGRectMake(500, 500, 600, 600);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rect.origin.x, rect.origin.y);
[yourView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You might also want to look at the -[UIView drawViewHierarchyInRect:afterScreenUpdates:] method. According to WWDC 2013 Session 226, “Implementing Engaging UI on iOS”, drawViewHierarchyInRect:afterScreenUpdates: is significantly faster than renderInContext:. See slide 41 for a speed comparison: 844 ms for the older method, and 145 ms for the newer method in their example.

Default iPhone screenshot programmatically

I tried two different ways to create a screenshot, but unfortunately they don't work as I need, I have an RMMapView that is blank on the screenshot. When I create snapshot manually on my device it works perfectly, and the map view is on the screen. So I would like to achieve the same result programmatically. Is it possible somehow as I tried? Or what is the right way to do that? (To reproduce that type of screenshot)
- (UIImage *) takeScreenshot {
//1. version
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
//2. version
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Actually You can use a method called takeSnapshot in a RMMapView.
UIImage *image = self.mapView.takeSnapshot
[Update]
Can you try this method instead
CGSize size = self.view.bounds.size;
CGRect cropRect = self.mapView.bounds
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect(mapImage.CGImage, cropRect);
UIImage * cropImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
UIImageWriteToSavedPhotosAlbum(cropImage, nil, nil, nil);
UIGraphicsEndImageContext();
Good luck

snapshotViewAfterScreenUpdates on UIScreen gives blank snapshot on device

I'm trying to get an image of the entire screen as the user sees it. The following code should work however it will only work in the simulator. What do I have to do to get this to work on a device running iOS 8.1?
UIView *snapshot = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
CGSize outputSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
UIGraphicsBeginImageContextWithOptions(outputSize, NO, 0);
[snapshot drawViewHierarchyInRect:CGRectMake(0.0, 0.0, outputSize.width, outputSize.height) afterScreenUpdates:YES];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Happy to try alternative strategies but the key is I want it as the user sees it - keyboards and all.
Cheers
UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)), NO, 1.0f);
[self.view drawViewHierarchyInRect:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) afterScreenUpdates:NO];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();

Take snap shot - only part of UIView, not the whole frame

I can take snapshots of the whole frame of the UIView with no problem.
Taking part of it cause to a squeezed image.
This is my UIView category method:
- (UIImage *)snapshot:(CGRect)frame
{
UIGraphicsBeginImageContextWithOptions(frame.size, YES, [[UIScreen mainScreen] scale]);
[self drawViewHierarchyInRect:frame afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
And this is the call to this method:
UIImage *img = [self.view snapshot:bottomRectToSnap];
When bottomRectToSnap is a partial frame of the view.
Note:The UIView contains UITableView if that matter.
The code you are applying is fine.
Need to add some logic, whatever part you need to take snap, It should be available on screen and hide rest of part while snapping.
Once snap shot done, remove hide code. So it will not effect to view interface too.
Use the following code.
- (void)captureView:(UIView*)view withFrame:(CGRect)frame{
UIGraphicsBeginImageContext(self.view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect = frame;
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
CGImageRelease(imageRef);
}
You can add this code to take the snapshot of particular part..
CGRect cropRect=CGRectMake(0, 0, 100, 100);
CGImageRef imageRef = CGImageCreateWithImageInRect([_galleryimageview.image CGImage], cropRect);
UIImage *cropedImage=[UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
This code crop the part of particular image ...Hope this is helpfull for you..

screen capture with frame

if you want to capture the screen is ios app , you can use the following codes:
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
currentCaptureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
but I want to capture the picture from a specific point, e.g. (start.x, start.y) and with a specific width and height, how can I do this ??
i just google it and got best answer From How to capture a specific size of the self.view
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
If your view was, for example, 600x320 and you wanted to capture the middle 300 points in width, you'd translate the context 150 points to the left:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, -150.f, 0.f);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Resources