For William & Mary’s Dress the Griffin app we offer the option to save the Griffin’s current outfit as an image to the iOS device’s photo album (which can then be shared via email or various social media outlets). I had a hard time finding a place that outlined these steps clearly, so here is the rundown of what needs to be done to programmatically save a full screen screenshot of your iOS app to the user’s photo album.
/* Action taken when the "Save" button (saveAsImageButton) is pressed in the app */ - (IBAction)saveScreenshot { // Define the dimensions of the screenshot you want to take (the entire screen in this case) CGSize size = [[UIScreen mainScreen] bounds].size; // Create the screenshot UIGraphicsBeginImageContext(size); // Put everything in the current view into the screenshot [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()]; // Save the current image context info into a UIImage UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // Save the screenshot to the device's photo album UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } // callback for UIImageWriteToSavedPhotosAlbum - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if (error) { // Handle if the image could not be saved to the photo album } else { // The save was successful and all is well } }
Note: The bulk of these functions are out of Apple’s UIKit framework so check out their documentation for even more information.
What about if you are showing a layer with the camera? I try to capture the screen, but it only captures the objects, not what the camera shows.
Thanks.
I’m not sure, maybe check the documentation for using the Camera interface? This snippet was focused on capturing programmatically generated UI rather than including the camera.
Thanks, it was really helpful.
Can we take successive screenshots and compose a video out of it?
If possible please let me know. I will let you know if I succeed.
You may be able to do something with looping the screenshot code to take successive screenshots. However, I’m not familiar with how you would string them together to make a video though inside the Android SDK (I know there are external apps that’ll do it but nothing I’m aware of that will do it inside another app, a Google search may help here). If you figure out a solution definitely let me know!
Above code works well but when try downscale the image little bit the uilabels are blurred even if i maintain the aspect ratio for resizing? what is the reason?
Glad the code work (mostly) for you. The blurred UILabels may be from the iOS library not handling the scaling of elements correctly when you resize. Have you tried just scaling the image outside of the app? Unfortunately I don’t have too many ideas of solutions for this (aside from a good ol’ Google search). If you do find out what’s causing the issue please add that in the comments.
Thank you! Although its a bit blurry when it takes the picture, this has been the only code I have found that actually works!
Have you used that code to save a screenshot of an arbitrary UIView that is smaller than the screen (e.g. size( w=1024, h=600))?
Unusual things occur when viewing that image in the Photos app….
I have not tried creating a screenshot that wasn’t the full screen size. What kind of unusual things are you seeing in Photos?