[Swift] Processing to share screenshots

What is this

For example, when you press the "share button", assume that a modal that takes a screenshot of the screen and posts it on Twitter etc. comes out from the bottom. Maybe you can copy and paste.

Method to take screenshot

  func takeScreenShot() {
    
    let width = CGFloat(UIScreen.main.bounds.size.width)
    //Calculate the height considering the part you do not want to copy, such as the status bar
    let height = CGFloat(UIScreen.main.bounds.size.height/1.3)
    let size = CGSize(width: width, height: height)
    
    UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
    
    //Export to view
    self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
    screenShotImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
  }

Share button processing

  //When you press the share button
  @IBAction func share(_ sender: Any) {
    
    //Take a screenshot
    takeScreenShot()
    
    let items = [screenShotImage] as [Any]
    
    //Share on the activity view
    let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
    
    //Modal appears
    present(activityVC, animated: true, completion: nil)
  }

Recommended Posts

[Swift] Processing to share screenshots
[Swift 5] Processing to close the keyboard on UITableView
[swift5] How to implement the Twitter share function
[Swift] Implement swipe processing
[Swift] Asynchronous processing "GCD"
[swift5] How to execute processing when tabBar is tapped
Easy to create Processing library
[Swift] How to use UserDefaults
[Swift] What is asynchronous processing?
[Swift] Share data using AppDelegate
[Swift] Asynchronous processing using PromiseKit
[Swift] Use UIProgressView to display progress + UIAlertController to notify processing completion
Pass C ++ char * to Swift
[Swift 5] Processing and precautions for transitioning to the iOS settings app
Swift: I want to chain arrays
Processing to issue an error message
Introduction to swift practice output Chapter5
[Swift] "for-in statement" about iterative processing
[Swift] Another way to use Result?
Delegate some Java processing to JavaScript
Introduction to Ruby processing system self-made
[Processing × Java] How to use variables
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
[Swift] How to send a notification
Manually perform processing equivalent to @ConfigurationProperties
Add processing to original annotation to Jackson
[Swift] How to replace multiple strings
[Processing × Java] How to use arrays
Convert Swift 2D array to C 2D array
[iOS] I tried to make a processing application like Instagram with Swift