12 Replies Latest reply: Jul 1, 2009 8:34 AM by fireman888
mike.mcfin Level 1 Level 1 (10 points)
Hello,

Is there a method to capture visible screen of my application from code?
I just need a pointer to UIImage or CGImage and can continue from there.

The problem is that I have multiple views over each other with varying alphas and combining them manually would be painful task.

Thanks.


mcMike

iMac 24", Mac OS X (10.5.4)
  • 1. Re: (iphone) application screenshot from code?
    RickMaddy Level 4 Level 4 (1,320 points)
    Here's code I use to convert a view into an image. If you pass it the top view of your app you have a screen capture:


    + (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();

    return newImage;
    }
  • 2. Re: (iphone) application screenshot from code?
    mike.mcfin Level 1 Level 1 (10 points)
    Thanks Rick.
    Problem solved. I just passed window to it and it captures everything.
  • 3. Re: (iphone) application screenshot from code?
    Doug Lerner2 Level 4 Level 4 (1,030 points)
    Rick,

    How would you refer to the "top view of your app", as you mentioned in what you pass to it?

    self.view? something with .superview in it?

    Thanks,

    doug
  • 4. Re: (iphone) application screenshot from code?
    Doug Lerner2 Level 4 Level 4 (1,030 points)
    Rick,

    If I try to compile your class method, at the following line:


    [view.layer renderInContext:ctx];


    I get a warning:

    warning: no '-renderinInContext' method found


    doug
  • 5. Re: (iphone) application screenshot from code?
    chataka Level 1 Level 1 (10 points)
    Include QuartzCore framework in your project and try again.
  • 6. Re: (iphone) application screenshot from code?
    Doug Lerner2 Level 4 Level 4 (1,030 points)
    Thanks for your reply chataka.

    In my MainViewController.h I added it as follows:


    #import <QuartzCore/QuartzCore.h>


    And that warning went away. Thanks!

    I'm still not sure how to call this method though...

    From another method I tried something like:


    UIImage *screenImage = [window captureView];


    which generates an error that window is not declared. I only tried that because somebody posted that they "passed window to captureView".

    How would I actually call capture? To what object?

    Thanks again?

    doug
  • 7. Re: (iphone) application screenshot from code?
    Doug Lerner2 Level 4 Level 4 (1,030 points)
    Well, I got it to work by rewriting Rick's code as an instance method responding to the pressing of a button. I still don't know how to use it, as Rick wrote, in order to pass it something and have his class method return the image.

    This routine, when assigned to a button, will take the current displayed view and capture it and save it to the photo library:


    - (IBAction)saveViewToPhotoLibrary:(id)sender {

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);

    [self.view.layer renderInContext:ctx];

    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIImageWriteToSavedPhotosAlbum(screenImage, nil, nil, nil);
    UIGraphicsEndImageContext();
    }


    doug
  • 8. Re: (iphone) application screenshot from code?
    Didier PULICANI Level 1 Level 1 (0 points)
    I have a question : is it possible to have a "full" view image, even if this view is hidden by a tabbar or else ? How can i change the "context" ?

    The target is to take a "picture" of a webpage, but only the visible part of the view are available (other are in black...)

    Thanks for your help !
  • 9. Re: (iphone) application screenshot from code?
    Jame Load Level 1 Level 1 (0 points)
    Does anyone know how to get a screenshot (programatically) of the homescreen (main screen with all the users icons)??

    Thanks!
  • 10. Re: (iphone) application screenshot from code?
    Kevin Lambert Level 1 Level 1 (0 points)
    Does anyone know how to get a screenshot (programatically)
    of the homescreen (main screen with all the users icons)??


    This is exactly what I'm trying to do as well... Anyone have a clue how to do this?
  • 11. Re: (iphone) application screenshot from code?
    vavdiyaharesh Level 1 Level 1 (25 points)
    Hello,


    CGRect screenRect = CGRectMake(50, 50, 320, 480);
    UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);
    [view.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;



    this code is working fine...but i don 't want tab bar and navigation bar,so i changed cgrect but this hides only tab bar...but navigation bar isstill display.....does any one have solved this issue? then pls let me know.

    Thanks.
  • 12. Re: (iphone) application screenshot from code?
    fireman888 Level 1 Level 1 (0 points)
    This is very good. But I have a question, I want to scale the screen capture to a smaller UIImage, say 200 by 200 pixels. Is there a easy way to do it?

    thanks,

    ff