views:

325

answers:

5

Is it possible in objective C that we can take the screen shot in objective c and stored this image in UIImage.

A: 

Yes, here's a link to the Apple Developer Forums https://devforums.apple.com/message/149553

MikeyWard
A: 

Hi Guys,

Only one question... how can I take a Snapshot, I would like to press a button and get a Screenshot of the iPhone display.

I have found this code, but I can't not put it under a button, how you can do that? **

 #import <QuartzCore/QuartzCore.h>

 - (UIImage*)captureView:(UIView *)view {
CGRect rect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}


 - (void)saveScreenshotToPhotosAlbum:(UIView *)view {
UIImageWriteToSavedPhotosAlbum([self captureView:view], nil, nil, nil);
}


 - (IBAction) doneButtonPressed: (id) sender {

        NSLog(@"Take a Screenshot.");

   **SEL sel = @selector(saveScreenshotToPhotosAlbum:view:);**

       [self dismissModalViewControllerAnimated:YES];
}

Regards!!!

Arthur Texada
Please don't post questions as answers to other questions. (Suggest you delete this response before someone down-votes you.)
Stephen Darlington
This is not exactly an answer to the question. You should formulate your problem with connecting this to a button more clearly and post it as a new question.
calmh
A: 

You need to create a bitmap context of the size of your screen and use

[self.view.layer renderInContext:c]

to copy your view in it. Once this is done, you can use

CGBitmapContextCreateImage(c)

to create a CGImage from your context.

Elaboration :

CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(ctx, 0.0, screenSize.height);
CGContextScaleCTM(ctx, 1.0, -1.0);

[(CALayer*)self.view.layer renderInContext:ctx];

CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
CGContextRelease(ctx);  
[UIImageJPEGRepresentation(image, 1.0) writeToFile:@"screen.jpg" atomically:NO];

Note that if you run your code in response to a click on a UIButton, your image will shows that button pressed.

VdesmedT
@VdesmedT: Can you elaborate here? Thanx
alis
+1  A: 

The previous code assumes that the view to be captured lives on the main screen...it might not.

Would this work to always capture the content of the main window? (warning: compiled in StackOverflow)


- (UIImage *) captureScreen {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CGRect rect = [keyWindow bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyWindow.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
Bill Garrison
+1  A: 

Technical Q&A QA1703 Screen Capture in UIKit Applications

http://developer.apple.com/iphone/library/qa/qa2010/qa1703.html

Bill Garrison