Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Possible to make a screenshot programmatically?

NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
edited May 2012 in iPhone SDK Development
Hi,

I was wondering if there was a way to make a screenshot programmatically and save it to the photo library?

Thanks in advance.
Post edited by NewiPhoneDeveloper on
Websites:<br />
<a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
<a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
<br />
iPhone Apps: <br />
<a hr
· ·
«1

Replies

  • mxweasmxweas Posts: 142Registered Users
    edited August 2008
    If you plan to distribute with app store this is not possible because you will be in a sandbox. If you plan to distribute for jailbroken phones placing images in /var/mobile/Media/DCIM/ will make them show in the camera roll.

    Max
    · ·
  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited August 2008
    Thanks for your answer! So, does this mean it is also not possible to do the screenshot on a regular iPhone? I mean without saving, maybe allowing the user to send it by email?

    Thanks!
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
    · ·
  • mxweasmxweas Posts: 142Registered Users
    edited August 2008
    I don't think there is a way at all of taking a screenshot without digging into some private api's

    Max
    · ·
  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited August 2008
    Yeah, you're right. Not too long ago I was wondering how these guys, who did the "Sketches app" were able to allow users to send custom graphics by email. Now I thought, maybe they took a screenshot and emailed it, but seems they did that in a different way. Man, I want to get behind that secret :confused:
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
    · ·
  • trappertrapper Posts: 299Registered Users
    edited August 2008
    I'm sure I've seen some apps that use a screen shot as their default.png? splashscreen shows exactly where you last were etc
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=294154344&mt=8" target="_blank">Black or Red</a> - Possibly the first ever iPhone drinking game! Try it tonight.<br />
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/
    · ·
  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited August 2008
    Ok, it seems there is at least some hope. So, the person who can tell me how to do this, will become my new best friend for sure :-)
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
    · ·
  • mortomorto Posts: 15Registered Users
    edited August 2008
    My English is very bad and i am not sure if I have understand everythign here.... but you guys want just a screencapture and save the image?!

    you can do this by pressing the powerbutton and home... it will make a screencapture and save it into the photo library...

    if this is somethign completly different then ur guys talking about... im sorry :)
    · ·
  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited August 2008
    Hehe :-) Thanks man, but I guess we all know that :-) We (or at least me) want to do it by code. So, no user interaction required :-)

    Thanks for trying to help though...Take care
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
    · ·
  • mortomorto Posts: 15Registered Users
    edited August 2008
    Oooo I see :)

    well... can´t help u there!
    · ·
  • ghostghost Posts: 272Registered Users @ @
    edited September 2008
    Has anyone gotten this working?
    · ·
  • ChriBChriB Posts: 154Registered Users
    edited September 2008
    Yeah, you're right. Not too long ago I was wondering how these guys, who did the "Sketches app" were able to allow users to send custom graphics by email. Now I thought, maybe they took a screenshot and emailed it, but seems they did that in a different way. Man, I want to get behind that secret :confused:

    Well... I think the Sketches.app is drawing to a CGContext. So if you need a picture of it the context is saved into an UIImage and after that saved somewhere so you can email it.
    But I don't know how you get an image without using a CGContext...
    Maybe the labels etc. are drawn into the actual view's context so you could use that one? I don't know...
    · ·
  • ghostghost Posts: 272Registered Users @ @
    edited September 2008
    Well I want to combine a label with an ImageView.
    Can I use CGContext and make the ImageView the background and draw a label onto it?
    · ·
  • bhearnbhearn Posts: 125Registered Users
    edited September 2008
    Hi,

    I was wondering if there was a way to make a screenshot programmatically and save it to the photo library?

    Thanks in advance.

    This works; I just tested it:
    #import <QuartzCore/QuartzCore.h>
    
    ...
    
    UIGraphicsBeginImageContext(self.window.bounds.size);
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    	
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    

    mxweas wrote: »
    If you plan to distribute with app store this is not possible because you will be in a sandbox. If you plan to distribute for jailbroken phones placing images in /var/mobile/Media/DCIM/ will make them show in the camera roll.

    Max

    This would be true, except that there's the UIKit function UIImageWriteToSavedPhotosAlbum().
    · ·
  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited September 2008
    Well... I think the Sketches.app is drawing to a CGContext. So if you need a picture of it the context is saved into an UIImage and after that saved somewhere so you can email it.

    Would you happen to have an example of that emailing procedure?

    Many thanks!
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
    · ·
  • ghostghost Posts: 272Registered Users @ @
    edited September 2008
    THANK YOU! So I'm using this code:
    UIGraphicsBeginImageContext(sizer.bounds.size);
    	[sizer.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    	UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    	UIGraphicsEndImageContext();
    	
    	UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    
    Where "sizer" is a UILabel that is over everything I want to take a picture of. Sizer is not at the top left of the screen - it is the width of the screen but starts about 50 pixels below the top. Whenever I take a screenshot, and image the size of sizer is saved, but it starts at the top left, meaning some of sizer gets cut off.
    Any advice?

    Thanks!!
    · ·
  • ChriBChriB Posts: 154Registered Users
    edited September 2008
    Would you happen to have an example of that emailing procedure?

    Many thanks!

    You can't attach files to an email from your app. So I think that would end up in making an own email function...
    Can't help sorry.
    · ·
  • NewiPhoneDeveloperNewiPhoneDeveloper Posts: 459Registered Users
    edited September 2008
    You can't attach files to an email from your app. So I think that would end up in making an own email function...
    Can't help sorry.

    Ok, I understand.

    I was under the impression, that there were apps, like "Sketches", that allowed the user to send images via email and that kinda implied to me that there was a hidden feature....
    Websites:<br />
    <a href="http://www.friendlydeveloper.com" target="_blank">Friendlydeveloper</a> - Coding Blog<br />
    <a href="http://www.codingsessions.com" target="_blank">Codingsessions</a> - Live iOS Training<br />
    <br />
    iPhone Apps: <br />
    <a hr
    · ·
  • ghostghost Posts: 272Registered Users @ @
    edited September 2008
    Nope, sketches only lets you export it to the photo album. No emailing.

    Back on topic ;), why won't the code I posted above let me select where I want to take the screenshot. it lets me select the size, but not the position :(
    · ·
  • bhearnbhearn Posts: 125Registered Users
    edited September 2008
    ghost wrote: »
    Nope, sketches only lets you export it to the photo album. No emailing.

    Back on topic ;), why won't the code I posted above let me select where I want to take the screenshot. it lets me select the size, but not the position :(

    Because you're rendering sizer.window.layer instead of sizer.layer. sizer.window.layer is simply the layer associated with the single iPhone window. And you're using the bounds of sizer instead of the window, so you only see a partial image.
    · ·
  • ghostghost Posts: 272Registered Users @ @
    edited September 2008
    But if I don't have "window", then my Jlabel won't show up. I want the window, but I want the capture area lowered.
    · ·
  • bhearnbhearn Posts: 125Registered Users
    edited September 2008
    Oops, I thought I'd replied to this already, but I must have forgotten to hit submit...

    OK, you want to use CGImageCreateWithImageInRect.

    Speficically,

    1. Make a UIImage of the whole window, as above
    2. Get the UIImage's CGImageRef
    3. Use CGImageCreateWithImageInRect to extract a region of interest as another CGImageRef
    4. Make a UIImage out of that
    5. Export to photo album
    · ·
  • ghostghost Posts: 272Registered Users @ @
    edited September 2008
    Thanks for your help! But I'm still a bit confused. So I use these two lines:
    UIGraphicsBeginImageContext(sizer.bounds.size);
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    Now what do I do before I call this line:
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    
    Or how do I extract the correct region? How do I use CGImageCreateWithImageInRect and CGImageRef? What exactly do I call these on?
    Sorry for all the questions...

    Thanks!
    · ·
  • bhearnbhearn Posts: 125Registered Users
    edited September 2008
    ghost wrote: »
    Thanks for your help! But I'm still a bit confused. So I use these two lines:
    UIGraphicsBeginImageContext(sizer.bounds.size);
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    

    No... you want to pass self.window.bounds.size to UIGraphicsBeginImageContext, to get the whole window contents to start with. Otherwise you won't get the area you are interested in.

    Now what do I do before I call this line:
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    

    Nothing. But after that, use viewImage.CGImage to get the image's underlying CGImageRef. Pass that to CGImageCreateWithImageInRect, which will give you a new CGImageRef. Finally, use + UIImage imageWithCGImage: on the new CGImageRef to get a final UIImage which is the contents of your area of interest.
    · ·
  • ghostghost Posts: 272Registered Users @ @
    edited September 2008
    ohhh that makes sense. Thanks!
    · ·
  • filharveyfilharvey Posts: 4New Users
    edited October 2008
    k, I'm trying the code suggested on a eaglView, but only getting a white image.
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [glView.layer renderInContext:ctx];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);

    So it saves an image, but the image is just white, not the opengl scene I've rendered.

    Thanks in advance for any help.

    Phil
    · ·
«1
Sign In or Register to comment.