iOS – TLB Entries https://archive.tiffanyb.net Musings on cooking, travel, social media, higher ed & web technology by Tiffany Broadbent Beker Tue, 28 Jun 2016 20:58:14 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.7 Programmatically taking a screenshot of your app in iOS https://archive.tiffanyb.net/2011/04/05/programmatically-taking-a-screenshot-of-your-app-in-ios/ https://archive.tiffanyb.net/2011/04/05/programmatically-taking-a-screenshot-of-your-app-in-ios/#comments Tue, 05 Apr 2011 19:57:33 +0000 http://tlbtechentries.wordpress.com/?p=12 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.

]]>
https://archive.tiffanyb.net/2011/04/05/programmatically-taking-a-screenshot-of-your-app-in-ios/feed/ 9