0

So I set up an UIImageView and a Label, and put them together to a single UIView(as my picture shown here)

enter image description 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. enter image description here

1 Answer 1

1

You are trying to create from view before it properly being rendered. You can create image in didAppear or when share button is clicked. Try this modified code.

 @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)

}
@IBAction func dismiss(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func share(sender: AnyObject) {

    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

    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)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.