ScreenshotSharer is a little Swift 4.2 pod that enables users to share a specific part of the view or the whole screen when they took a screenshot. It's heavily inspired from Asos app and highly customizable.
pod 'ScreenshotSharer'
Just download or clone the repo and move Classes folder to your project.
You can init a new ScreeshotSharer instance to register a view or window to be captured.
import UIKit
import ScreenshotSharer
class ViewController: UIViewController {
let sssharer = ScreenshotSharer()
@IBOutlet weak var captureView: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
sssharer.registerViewCapturer(view: self.captureView, cropRect: CGRect.zero, captureBlock: { (image, screenshotSharerViewController) in
//this block is called when the user takes a screenshot
//image is the image of given view and it can be cropped according to cropRect.
//sharerViewController is the presented view controller
}) { (isSuccess) in
//this block is called when sharerViewController is dismissed
//isSuccess indicates if sharing is completed successfully.
}
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
sssharer.isEnabled = true
}
override func viewWillDisappear(_ animated: Bool)
{
super.viewWillAppear(animated)
sssharer.isEnabled = false
}
}
sssharer.registerScreenCapturer(cropStatusBar: true, cropRect: CGRect.zero, captureBlock: { (image, screenshotSharerViewController) in
//this block is called when the user takes a screenshot
//image is the image of given view and it can be cropped according to cropRect.
//sharerViewController is the presented view controller
}) { (isSuccess) in
//this block is called when sharerViewController is dismissed
//isSuccess indicates if sharing is completed successfully.
}
By default ScreenshotSharer uses ScreenshotSharerMinimal as the presented sharer view controller. You can customize it in the captureBlock:
sssharer.registerScreenCapturer(cropStatusBar: true, cropRect: CGRect.zero, captureBlock: { (image, screenshotSharerViewController) in
if let sharerViewController = sharerViewController
{
sharerViewController.view.backgroundColor = UIColor.red
sharerViewController.setShareTitleText(_ text: String)
sharerViewController.setShareTitleFont(_ font: UIFont)
sharerViewController.setShareTitleColor(_ color: UIColor)
}
}) { (isSuccess) in
}
These are the all the methods you can use to customize default sharer view controller:
func setShareTitleText(_ text:String)
func setShareDescriptionText(_ text:String)
func setShareButtonTitleText(_ text:String)
func setShareTitleFont(_ font:UIFont)
func setShareDescriptionFont(_ font:UIFont)
func setShareButtonTitleFont(_ font:UIFont)
func setShareTitleTextColor(_ color:UIColor)
func setShareDescriptionTextColor(_ color:UIColor)
func setShareButtonTitleColor(_ color:UIColor)
func setShareButtonBackgroundColor(_ color:UIColor)
In some cases you may want to design the whole sharer view controller from stratch. To do this your sharer view controller should extend ScreenshotSharerViewController class and you should register it to ScreenshotSharer instance. Default sharer view controller uses UIActivityViewController but you can implement your own share logic.
let customSharer = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CustomSharerViewController") as! CustomSharerViewController
sssharer.registerScreenCapturer(cropStatusBar: true, cropRect: CGRect.zero, sharerViewController: customSharer, captureBlock: { (image, customScreenshotSharerViewController) in
}) { (isSuccess) in
}
You can use the ScreenshotSharerMinimal.swift and ScreenshotSharerMinimal.xib files as your base design. ScreenshotSharer will present your own view controller and call this method:
func setScreenshotImage(_ image:UIImage)
Therefore you should implement setScreenshotImage(_ image:UIImage)
method. When you want to dismiss the sharer view controller you should call this method in your own sharer view controller:
self.screenshotSharer()?.dismissSharerViewController(isSuccess)
isSuccess
indicates that sharing is completed successfully.
MIT
Free Software, Hell Yeah!