Get screenshot of current screen in iOS/swift for sharing [duplicate] - ios

This question already has answers here:
Screenshot in Swift iOS?
(4 answers)
Closed 5 years ago.
I would like to enable a feature on my phone that allows users to click share, and then they would be able to text their friends a message that by default included a picture of the app (right before the user hit share). I cannot figure out how to do this - and whenever I search on how to do it all I get are tutorials/answers on how to share images in the phone's photo library or by their camera. Any idea on how to do this!

// to create an image from screenshot
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// to share the image
var imagesToShare = [AnyObject]()
imagesToShare.append(image)
let activityViewController = UIActivityViewController(activityItems: imagesToShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
presentViewController(activityViewController, animated: true, completion: nil)

UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

To share your screen just before user hits Share , first you will be needing to change your screen's view into an image and sharing it.
To get change your view into an image you can add this extension in your code.
//UIView extension which converts the UIView into an image.
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Pass your ViewController's view there , for example :
let imageToShare = self.view.toImage()
let activityItems : NSMutableArray = []()
activityItems.addObject(imageToShare)
let activityVC = UIActivityViewController(activityItems:activityItems as [AnyObject] , applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
I hope this helps you out.

Related

How to show URL icon with text in UIActivityViewController?

I have tried different ways to show a URL icon with text but didnt succeed. Here is the code and its output snippets
Example 1:
let text = "I am text"
let myWebsite = URL(string:"https://www.youtube.com/")
let shareAll = [text, myWebsite] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Output 1:
UIActivity View Messages View
Example 2:
let myWebsite = URL(string:"https://www.youtube.com/")
let shareAll = [myWebsite] as [Any]
let activityViewController = UIActivityViewController(activityItems: shareAll, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
Output 2:
UIActivity View
Required:
I want URL icon like in Output 2 for Example 1.
There is a difference between sharing data and showing data - I think in your case, showing the data that's about to be shared.
I could be wrong, but if not, you are actually able to share the data - in this case a UIImage and text - but wish to display what is about to be shared. I had this issue a while back. Here's my code. See if it can help you - basically you need to add metadata to UIActivityViewController delegate.
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let shareImage = preview.uiImage.adjustedForShareSheetPreviewIconProvider()
let imageProvider = NSItemProvider(object: shareImage)
let metadata = LPLinkMetadata()
metadata.imageProvider = imageProvider
metadata.title = "Title"
return metadata
}
Adjust shareImage and title as needed.
EDIT
I was using my code and was incomplete. adjustedForShareSheetPreviewIconProvider() is part of UIImage, and as such, it assumes this. In my code, I'm actually calling a GLKView to return this UIImage and then use it this way in the metadata.
So if your image is something returned from the URL as an icon and is (or can be converted to) a UIImage, just add it to the metadata like my code above.

Can't sharing video from camera roll

I have an application which collects the video assets of a users camera roll, using the Photos Framework and methods requestImage & requestAVAsset
I'm allowing the user to share the asset using the UIActivityViewController class and the URL from the AVAsset. My problem is that for some strange reason it won't allow me to share the video.
On the app the error message I get on the UIActivityViewController is
The item cannot be shared. Please select a different item.
Any pointers or advise is appreciated, thanks!
Here is how I present the UIActivityViewController:
let selectedVideo = self.selectedVideo //(AVAsset)
let videoURLPath = selectedVideo.url // URL
DispatchQueue.main.async {
let activityItems: [Any] = [videoURLPath]
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.airDrop, UIActivity.ActivityType.assignToContact,]
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.popoverPresentationController?.sourceRect = self.view.frame
self.present(activityViewController, animated: true, completion: nil)
}
Here's how the video URL looks:
file:///var/mobile/Media/DCIM/107APPLE/IMG_7966.MP4

Pictures taken with the camera in my app are upside down [duplicate]

This question already has an answer here:
PDFKit is vertically flipping PDFPage initialised with image
(1 answer)
Closed 5 years ago.
When I use the camera to take a picture in my app the images are then displayed upside down.
I use a UIImagePickerController to take a photo and just select the one i am happy with. Upon selection, the following function is execute:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("image selected ...")
// Get picked image info dictionary
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
// Image is upside down. Need to re-orient
// Add captured and selected image to your Photo Library
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
// Create PDFDocument object and display in PDFView
let document = createPDFDataFromImage(image: image)
myPDFView.document = document
// Dimiss imagePicker
dismiss(animated: true, completion: nil)
}
func createPDFDataFromImage(image: UIImage) -> PDFDocument{
let document = PDFDocument.init()
let imagePDF = PDFPage.init(image: image)
document.insert(imagePDF!, at: 0)
return document
}
How can I stop the pictures from being upside down?
Here is one way to do it:
var flippedImage: UIImage?
if let origImage = UIImage(named: "s1") {
if let cg = origImage.cgImage {
flippedImage = UIImage(cgImage: cg, scale: 1.0, orientation: UIImageOrientation.downMirrored)
}
}
theImageView.image = flippedImage

(Swift) Sharing on Facebook set initial text?

The code that I am using prepares a screenshot and opens the share window with the screenshot. However, the setInitialText does not seem to work. At least anything I enter in it is not appearing. The root does not state that it is depreciated.
let screen = UIScreen.mainScreen()
if let window = UIApplication.sharedApplication().keyWindow {
UIGraphicsBeginImageContextWithOptions(screen.bounds.size, false, 0);
window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
composeSheet.setInitialText("I just hit \(highscore) on Reflext Test! Can you beat me?")
composeSheet.addImage(image)
presentViewController(composeSheet, animated: true, completion: nil)
}
The problem is now Facebook doesn't allow pre-filled text to be posted/shared. This link will provide you information about Facebook pre-filled text policy.
https://developers.facebook.com/docs/apps/review/prefill

How do I capture screen view and share it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have come across a few posts in different places, but do not think they fully cover what I am looking for.
By tapping a bar button item called 'Share', I would like my app to take a screenshot of the current view in the app and pop the sharing window up to share it with Twitter, Facebook and the rest of them.
You can add share button
var shareButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "shareButtonPressed:")
self.navigationItem.rightBarButtonItem = shareButton
and populate share options
func shareButtonPressed(sender: AnyObject) {
NSLog("shareButton pressed")
let stringtoshare: String = "This is a string to share"
//add current screen shot image to share
let imagetoshare = captureScreen()
let activityItems: [AnyObject] = [stringtoshare, imagetoshare]
let activityVC: UIActivityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityVC.excludedActivityTypes = [UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo]
self.presentViewController(activityVC, animated: TRUE, completion: { _ in })
}
//Capture screen shot image
func captureScreen() -> UIImage
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}
Please let me know if you find any trouble in this, I am not able to test this code snip (as I am not at system) but this is how i used to handle in apps.

Resources