Dress the Griffin on the iPadFor 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.

More