Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-19-2009, 01:12 PM   #1 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 19
matthewfelton is on a distinguished road
Default screen capture from UIbutton

hi people, how can i take screenshots of the current view in my app. and link it to an action/UIButton. i know i could get the user to just use home and top button but they'll be doing that loads of times. all i really need to know is whats the function to handle this sort of thing. cheers in advance.
matthewfelton is offline   Reply With Quote
Old 04-20-2009, 12:06 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 8
shadyrip is on a distinguished road
Default

Hey, I also need that function but I can't find any documentations about it. Is someone can help us plz ?
shadyrip is offline   Reply With Quote
Old 04-27-2009, 03:39 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 46
peachGames is on a distinguished road
Default

Hello..

I also need this. Is there anybody who has an idea?
peachGames is offline   Reply With Quote
Old 04-27-2009, 06:02 PM   #4 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,971
Bertrand21 is on a distinguished road
Default

I know how to do it. Ill post some code after work
__________________
Haters gonna Hate
Likers gonna Like
Bertrand21 is offline   Reply With Quote
Old 04-28-2009, 05:12 AM   #5 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 19
matthewfelton is on a distinguished road
Default

Ive got it.

in the .h file import

Code:
#import <QuartzCore/QuartzCore.h>
then you can add this to an action to get it to take a screenshot

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

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

[self.view.layer renderInContext:ctx];

UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);
UIGraphicsEndImageContext();
hope this helps the rest of you...

you might also need to import coregraphics.framework which you can find by:

project/ add to project / system/library/frameworks

Last edited by matthewfelton; 04-28-2009 at 03:58 PM.
matthewfelton is offline   Reply With Quote
Old 07-28-2009, 01:30 AM   #6 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 57
Coaster is on a distinguished road
Default

That code works awesome thanks mate,

Is there a way to increase the pixel count of a screenshot? I would like to print a screenshot photo on to a piece of paper, but when i do, the image appears fuzzy, and i believe it is because the screenshot is being saved in the lowest possible pixel count.
Coaster is offline   Reply With Quote
Old 04-04-2010, 12:23 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 11
simond is on a distinguished road
Default

Quote:
Originally Posted by matthewfelton View Post
Ive got it.

in the .h file import

Code:
#import <QuartzCore/QuartzCore.h>
then you can add this to an action to get it to take a screenshot

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

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

[self.view.layer renderInContext:ctx];

UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);
UIGraphicsEndImageContext();
hope this helps the rest of you...

you might also need to import coregraphics.framework which you can find by:

project/ add to project / system/library/frameworks
Thank you, matthewfelton!
Your code was a big help - so quick and easy to implement!!
-SD
simond is offline   Reply With Quote
Old 07-08-2010, 10:31 AM   #8 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 105
daveMac is on a distinguished road
Default Thank You!

This is just what I needed. I appreciate it!
daveMac is offline   Reply With Quote
Old 08-09-2010, 04:35 PM   #9 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 12
MrGreatTutorials is on a distinguished road
Default

Quote:
Originally Posted by matthewfelton View Post
Ive got it.

in the .h file import

Code:
#import <QuartzCore/QuartzCore.h>
then you can add this to an action to get it to take a screenshot

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

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

[self.view.layer renderInContext:ctx];

UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image1, nil, nil, nil);
UIGraphicsEndImageContext();
hope this helps the rest of you...

you might also need to import coregraphics.framework which you can find by:

project/ add to project / system/library/frameworks
Hey, it works perfect, but my App is in Landscape and it takes the screen shot in portrait mode, is there any more code i should put in to make it work with landscape?
MrGreatTutorials is offline   Reply With Quote
Old 09-10-2010, 01:07 PM   #10 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 105
daveMac is on a distinguished road
Default

Quote:
Originally Posted by MrGreatTutorials View Post
Hey, it works perfect, but my App is in Landscape and it takes the screen shot in portrait mode, is there any more code i should put in to make it work with landscape?
I actually need this in my app now too. Did anyone figure this out?
daveMac is offline   Reply With Quote
Old 09-10-2010, 05:38 PM   #11 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 105
daveMac is on a distinguished road
Default Got it!

Ok, despite the fact that most of the posts on the internet pertaining to this subject make it sound quite difficult to use this code in landscape mode, it is actually extremely easy. Just change the first line of this function:

Code:
CGRect screenRect = [[UIScreen mainScreen] bounds];
Depending on what kind of view you need to use this in, just change the rect. I am using this in a UIViewController, but it would work just the same for a regular UIView (just omit the ".view"). Change the above line of code to:

Code:
CGRect screenRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.bounds.size.width, self.view.bounds.size.height);
You can also specifically set each parameter to your own dimensions:

Code:
CGRect screenRect = CGRectMake(0, 0, 480, 320);
However this is strongly advised against due to the fact that it offers absolutely no flexibility for any changes that might occur in new devices or updates. It can open up the door to potential problems down the road. Hope this helps anyone who might be needing it.
daveMac is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 322
7 members and 315 guests
blueorb, guusleijsten, jbro, Kryckter, mer10, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:59 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0