How to take a picture of a UIView? - ios

I have an app where I want to take a screenshot of a UIView when I am in another view. I have 4 views on one of my pages that can be toggled using a segmented control. But I want bmy view in the second segment to e displayed when I am in the first segment. Is there any way that I can do this?
This is what I have used so far:
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect;
rect=CGRectMake(0, 120, 320, 560);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
Screenshot = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return Screenshot;
This gets me the screenshot of the view I currently am in.
What I want to do is to take a screenshot of the second view while in the first view.
This is what I changed:
UIGraphicsBeginImageContext(View2.bounds.size);
For some reason this is not working? Can you tell me why and how to fix it?

You need to change [self.view.layer renderInContext:context]; to [self.view2.layer renderInContext:context];
UIGraphicsBeginImageContext(View2.bounds.size); Only specifies the bounds to render the view!

Related

UINavigationBar appears gray in screenshot

I am taking a screenshot of my app's current view using the code below. I have a UIViewController embedded inside a UINavigationController. The method is being called in the UIViewController.
In the screenshot, the navigation bar is colored gray even though the barTintColor is set to another color. Why is this happening?
-(UIImage *)generateScreenshot {
CGFloat scale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
I think the problem should be that you are rendering a view which doesn't include the navigation bar,Replace [self.view.layer renderInContext:context]; with
[[appDelegate window].layer renderInContext:context];
Hope It Helps...:)

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

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..

How to capture UIImage of the visible part of tableView

I am first to use UIGraphicsBeginImageContext.
I want to capture the visible part of the tableView to an UIImage and let it to the content of a new CALayer.
Here is my code:
UIGraphicsBeginImageContext(tableView.frame.size);
[tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect frame = [self.view.window convertRect:view.frame fromView:tableView.superview];
CALayer *imageLayer = [CALayer layer];
imageLayer.contents = (id)image.CGImage;
imageLayer.frame = frame;
However, if I scroll down the tableView, the top part of captured image will be white.
I use UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); to see the result.
After longtime finding the answer, I have to ask this question here, hope someone can help me!
Thanks!
Make a view called "viewHolder", set it's "clipsToBounds" to yes. Add tableView as a subview to viewHolder. Now use:
UIGraphicsBeginImageContext(viewHolder.frame.size);
[viewHolder.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Taking screenshot of NGLView (NineVehGL engine)

I am using NinevehGL 3D engine http://nineveh.gl/
I want to get screenshot of OpenGL object. Problem is with below code
-(UIImage *) screenshot
{
// Capture screen here... and cut the appropriate size for saving and uploading
UIGraphicsBeginImageContext([self theNGLView].bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// crop the area you want
CGRect rect;
rect = CGRectMake(0, 10, 300, 300); // whatever you want
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
Only background comes in screenshot but not actual NGLView or OpenGL object which is drawn on top of it.
I am using iOS SDK 6.0
tried using
[[self the NGLView].layer renderInContext:UIGraphicsGetCurrentContext()];
?
Please go through this link, you need to download the latest NinevehGL SDK for iPhone5/iOS 6 which is provided in their twitter link.

Resources