InnerShot — Capturing Screenshots and Saving to Photos Library in 5 Step (iOS-Swift)

Uchiha
3 min readJul 31, 2023

--

In this blog post, we will explore how to implement a feature in the InnerShot app that allows users to take a screenshot of the current screen and save it directly to their Photos library. We will use Swift and UIKit to achieve this functionality. This blog assumes a basic understanding of iOS app development and Swift programming.

Introduction

The InnerShot app is designed to provide users with a convenient way to capture screenshots and store them in their device’s Photos library. We will create a simple button in the app’s main view controller that triggers the screenshot-taking functionality.

innerShot

Step 1: Setting up the Project

Create a new Xcode project.

Step 2: Adding the Button and Action

Open the `Main.storyboard` file and add a button to the view controller’s interface. Connect the button to the `btnTakeScreenshot(_:)` action in the `ViewController.swift` file.

@IBAction func btnTakeScreenshot(_ sender: Any) {
takeScreenshotAndSave()
}

Step 3: Taking the Screenshot

In the `ViewController.swift` file, implement the `takeScreenshotAndSave()` function. This function captures the screen as an image using `UIGraphicsImageRenderer` and saves it to the `Photos` library using `UIImageWriteToSavedPhotosAlbum`.

 func takeScreenshotAndSave() {

guard let view = UIApplication.shared.windows.first?.rootViewController?.view else {
return
}

// Create an image renderer
let renderer = UIGraphicsImageRenderer(size: view.bounds.size)

// Render the view into an image
let screenshotImage = renderer.image { context in
view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
}

// Save the screenshot to the Photos library
UIImageWriteToSavedPhotosAlbum(screenshotImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
}

Step 4: Handling the Save Result

To handle the result of saving the screenshot to the Photos library, we add an extension to the `ViewController` class. The `image(_:didFinishSavingWithError:contextInfo:)` method will be called after the image is saved, where we can display a toast message to inform the user about the status.

@objc func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeMutableRawPointer?) {
if let error = error {
// Handle error saving the image
showToast(message: "Error saving image: \(error.localizedDescription)")
} else {
// Image saved successfully
showToast(message: "ScreenShot Taken")
}
}

Step 5: Displaying Toast Message

The `showToast(message:)` function creates and presents a `UIAlertController` as a toast message to the user. It automatically dismisses the message after a short delay.

func showToast(message: String) {
let toast = UIAlertController(title: nil, message: message, preferredStyle: .alert)
UIApplication.shared.keyWindow?.rootViewController?.present(toast, animated: true, completion: {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
toast.dismiss(animated: true)
}
})
}

Conclusion

With these simple steps, Successfully implemented a screenshot-taking and saving feature in the InnerShot app. Users can now take a screenshot of the app’s screen with a single tap and have it saved directly to their Photos library.

Final Thoughts

Adding screenshot functionality to your app can greatly enhance the user experience and allow users to easily capture important information. It’s a handy feature for various types of apps, such as productivity, note-taking, or image-related apps. Always remember to handle user permissions and errors appropriately to ensure a smooth user experience.

You can find the complete source code for the InnerShot app in the [GitHub repository](https://github.com/meet4877/InnerShot).

I hope you found this blog post helpful. Happy coding! 😄

--

--

Uchiha
Uchiha

Written by Uchiha

iOS Developer & Swift enthusiast

No responses yet