Wylie Johnston
Wylie Johnston

Reputation: 17

Using Swift UIActivityViewController to share image from separate Class(viewcontroller)?

I have a game in swift. I want a custom image for users to be able to share highscores to Facebook, Twitter etc.

Something like this would be on a separate view controller.

Something like this would be on a separate view controller from where I would share the image from.

I have created this image in a separate view controller. From my share button, I want to pass the Username, High Score etc for the labels in this separate view controller. I then want to capture the image(screenshot) of this view controller and using the UIActivityController allow users to share it

This view controller with the high scores should never be displayed. I just want a image(screenshot) captured of it.

Upvotes: 0

Views: 366

Answers (1)

Max
Max

Reputation: 1163

You shouldn't use a view controller for this. Just make a UIView that contains the labels and then create an image from the view using something like this:

func snapshot(view: UIView) {
    UIGraphicsBeginImageContext(CGSizeMake(100, 100))
    let context: CGContextRef = UIGraphicsGetCurrentContext()
    view.layer.renderInContext(context)
    let screenShot: UIImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

Upvotes: 1

Related Questions