So I set up an UIImageView and a Label, and put them together to a single UIView(as my picture shown here)
And I did the following to render this view to an UIImage, and share it which will bring the UIActivityViewController.
@IBOutlet var viewBack: UIView!
@IBOutlet var dismissButton: UIButton!
@IBOutlet var shareButton: UIButton!
@IBOutlet var imageView: UIImageView!
@IBOutlet var hello: UILabel!
var image = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
shareButton.backgroundColor = UIColor.grayColor()
shareButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
shareButton.layer.cornerRadius = 10
// Do any additional setup after loading the view.
dismissButton.backgroundColor = UIColor.grayColor()
dismissButton.layer.cornerRadius = 10
viewBack.backgroundColor = UIColor(white: 1, alpha: 0.0)
UIGraphicsBeginImageContextWithOptions(viewBack.bounds.size, viewBack.opaque, 0.0)
viewBack.drawViewHierarchyInRect(viewBack.bounds, afterScreenUpdates: false)
imageView.drawViewHierarchyInRect(imageView.frame, afterScreenUpdates: false)
hello.drawViewHierarchyInRect(hello.frame, afterScreenUpdates: false)
let snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
print(snapshotImageFromMyView)
image = snapshotImageFromMyView
}
@IBAction func dismiss(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func share(sender: AnyObject) {
var shareArray : [AnyObject] = []
shareArray.append(image)
let activityVC = UIActivityViewController(activityItems: shareArray, applicationActivities: nil)
if let popover = activityVC.popoverPresentationController{
popover.sourceView = sender as? UIView
popover.sourceRect = sender.bounds
}
self.presentViewController(activityVC, animated: true, completion: nil)
}
So viewBack is the UIView contains two subviews- The UIImage and the label. And it seems to be that I set up all the rendering procedure normally. But when I actually share it, the result is really strange, a black picture. Apparently I set my image view to a specific image, and label isn't empty as well. I don't know what's wrong here, hopefully somebody could help me. Thanks in advance.

