How to take a screenshot programmatically in iOS? - iphone

I have a UIView that uses both UIKit control and OpenGL. I'd like to get a screenshot of that view programatically.
If I use UIGraphicsGetImageFromCurrentImageContext(), the OpenGL content is blank;
If I use the glReadPixels(...) method, the UIKit content is blank;
I'm confused as how to take a complete screenshot.
Thank you!

use following code to take screen shot
-(UIImage *) screenshot
{
CGRect rect;
rect=CGRectMake(0, 0, 320, 480);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

-(UIImage *) screenshot
{
CGImageRef UIGetScreenImage(void);
CGImageRef screen = UIGetScreenImage();
UIImage* screenImage = [UIImage imageWithCGImage:screen];
CGImageRelease(screen); // you need to call this.
return screenImage;
}

Well, there are few ways of capturing the iPhone screen programmatically
Using UIKIT http://developer.apple.com/library/ios/#qa/qa1703/_index.html
Using AVFoundation framework http://developer.apple.com/library/ios/#qa/qa1702/_index.html
Using OpenGL ES
http://developer.apple.com/library/ios/#qa/qa1704/_index.html
Starting from iOS 7 you can also use Why does my programmatically created screenshot look so bad on iOS 7?
Using UIWindow
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.window.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Using UIView
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Out of these 6 options, I found the first option very convenient for copy-pasting and for applying level of compression as 1st method gives the image with true pixel data. I also like option 4, as the API comes with SDK.

You get result Uiimage without lose quality of image
- (UIImage *)captureView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = [UIImagePNGRepresentation(image) retain];
UImage *screenShot = [UIImage imageWithData:data];

You know where the OpenGL content is located in your view. So it should be easy to copy the result of glReadPixels into the snapshot of the UIKit.

maybe.. can you use something from here
- https://stackoverflow.com/questions/12413460/cocos2d-2-0-screenshots-on-ios-6
and here...
Why is glReadPixels() failing in this code in iOS 6.0?

Here is how you can take a simple screenshot programmatically of a UIImage. If you want it for any other object substitute the UIImage with it.
In the .h file add NSData *Data; then in your .m file add this to your code
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *attachimage = [[UIImage alloc]initWithData:Data];
UIImage *viImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
attachimage = viImage;
UIImageWriteToSavedPhotosAlbum(viImage, nil, nil, nil);

Related

Turning a UITableView into an UIImageView

I have got a UITableView, and I want to capture the visible portion of it, and put it into a UIImage. Any way of doing this?
EDIT
Self-answered below
Thanks for the replies. All of the answers would work with a UIView, but not with a UITableView - at least not when the table has scrolled. When scrolled, the captured image would appear black or transparent. I had tried similar solutions as the ones below before. I guess people assumed the answer was obvious, and that's why I was down-voted - for being naughty enough to ask such an obvious question.
Anyway, the real solution is that you have to use the table's contentOffset:
- (UIImage *) imageWithTableView:(UITableView *)tableView
{
UIView *renderedView = tableView;
CGPoint tableContentOffset = tableView.contentOffset;
UIGraphicsBeginImageContextWithOptions(renderedView.bounds.size, renderedView.opaque, 0.0);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(contextRef, 0, -tableContentOffset.y);
[tableView.layer renderInContext:contextRef];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UIGraphicsBeginImageContext(someView.bounds.size);
[someView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Taken from this question.
- (UIImage *)captureView {
//hide controls if needed
CGRect rect = [yourTableView bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[yourTableview.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
UIGraphicsBeginImageContext(tableView.bounds.size);
[tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Maybe those posts will be helpful
Rendering UIView with its children
Saving UIView contents in iOS 4 with real size of the images inside (i.e. scale contentes up for save)
Safe way to render UIVIew to an image on background thread?

UIImage screenshot loses its quality

I am merging two images and then I take a screenshot by applying this code:
UIGraphicsBeginImageContext(size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img_AddText=viewImage;
[dragView removeFromSuperview];
imgV_SelectedImg.image=nil;
imgV_SelectedImg.image=img_AddText;
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
The problem is that when the final image loses its quality it blurs.
Try using the withOptions version of UIGraphicsBeginImageContext
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
i got the snapshot with good quality and particular location of screen. By this code.
-(UIImage *)takeScreenShot
{
CGRect grabRect;
grabRect = CGRectMake(0,70,320,260);
UIGraphicsBeginImageContextWithOptions(grabRect.size, self.view.opaque, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y);
[self.view.layer renderInContext:ctx];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
it gives me excellent snapshot..
I've made a category on UIImage class that may help you. It goes like this:
+ (UIImage*)imageWithView:(UIView *)view opaque:(BOOL)opaque bgColor:(UIColor*)bgColor{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, opaque, [[UIScreen mainScreen] scale]);
if(!opaque){
[bgColor set];
}
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
It works fine for me. No bluring was detected. Try to use it. If you'll still have it then most likely the problem is in your saving code...
Cheers... :)
UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
this solve my prblem by increasing scale from 1.0 to 2.0
Did you provide an image for retina display? You should check it. You might be running in a simulator (in retina).

how to get snap shots of touch part on screen by programming?

I am implementing iphone application in which I want to implement below features.
When user touch on iphone screen then user snap shot will generate of touch area of the screen and save to photo library.
I have done googling but dont get successed.
Please help me for this query.
Thanks in advance
If you are looking for sample code to control the camera. Here is a bare bones Camera application that takes picture and saves it to library
Check out this method, pass the view touched to this method, will send u the image & save it to library.
- (UIImage*) giveScreentshotsOfView:(UIView*) view
{
if (view == nil)
return nil;
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *) captureView:(UIView *)view
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[view.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//save the image to photo album
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil , nil);
}

Capturing screenshot in iphone?

I want to take screenshot programatically. So that i write code for it like,
CGContextRef context = [self createBitmapContextOfSize:self.frame.size];
//not sure why this is necessary...image renders upside-down and mirrored
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, self.frame.size.height);
CGContextConcatCTM(context, flipVertical);
[self.layer renderInContext:context];
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* background = [UIImage imageWithCGImage: cgImage];
CGImageRelease(cgImage);
self.currentScreen = background;
Here all code is in custom UIView of UIViewController. Now when i play UIImageView png sequence animation on UIView, then i didn't get updated changes in UIImageView which is subview of custom UIView, why? I got only result is UIImageView with first UIImage.
Basically i need to create an video of my game play like talking tom application.
Take the screenshot of your ImageView with something like this:
[[[yourImageView layer] presentationLayer] renderInContext:context];
Use NSTimer for calling that getScreenshot function.
Hope this helps
you should probably take a look at AVFoundation and specifically AVAssetWriter as a way of creating videos of your screen content.
Following code is use for the capture the image into the iPhone for that following code to be use.
CGRect rect = CGRectMake(0.0, 0.0,self.view.frame.size.width,self.view.frame.size.height-44);
NSValue *rectObj = [NSValue valueWithCGRect:rect];
CGRect rectRestored = [rectObj CGRectValue];
UIGraphicsBeginImageContext(CGSizeMake(rectRestored.size.width, rectRestored.size.height-44));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
Also store the image into the gallery.

iPhone: UIImage obtained after rendering a view is solid black

I'm using this method to render a UIView into a UIImage:
+ (UIImage *)imageWithView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
The resulting image is of the right size (as shown by the UIImage's size property), but its contents is solid black colour. The view passed there definitely contains some graphics, but it's not rendered. Any idea why?
This is the code I use to capture images of a UIView:
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
Your code looks fine to me. You can try swapping mine out to see if it helps, but I'd cite it to be a separate issue from the code you posted.