Save image to library from URL, rename, and share it using Swift - ios

I want to share images to Instagram from my own iOS app and I am using Kingfisher throughout the project to download and cache images and show them in UIImageViews but this time I want to do something a little bit different.
Basically, I am getting a response from an API with the url of an image and I want to
Download the image to the library using the URL
There is a bunch of questions on this for objective-C using
UIImageWriteToSavedPhotosAlbum
but I am using Swift.
Rename it with .igo extension (instagram exclusive)
Not sure how to go about this, would depend on number 1.
Then I could share it doing something like
let image = UIImage(named: "downloadedImage")
let objectsToShare: [AnyObject] = [ image! ]
let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ]
self.presentViewController(activityViewController, animated: true, completion: nil)
or I could do it using the instagram hooks:
instagram://library?LocalIdentifier=\(localID)
Documentation on this is scarce specially for Swift. How can I go about this? a push in the right direction is all I need.

It sounds like you already know how to use Kingfisher and retrieving the UIImage from the URL. So what i'm about to provide is information for saving the image to the documents directory and retrieving it to share.
Retrieve the correct directory URL
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
Saving an image to that directory
func saveImage (image: UIImage, filename: String ){
print("Saving image with name \(filename)")
if let data = UIImagePNGRepresentation(image) {
let fullURL = getDocumentsDirectory().appendingPathComponent(filename)
try? data.write(to: fullURL)
}
}
Retrieving an image from the directory
func loadImageFromName(name: String) -> UIImage? {
print("Loading image with name \(name)")
let path = getDocumentsDirectory().appendingPathComponent(name).path
let image = UIImage(contentsOfFile: path)
if image == nil {
print("missing image at: \(path)")
}
return image
}
Sharing an image
func share(shareText shareText:String?,shareImage:UIImage?){
var objectsToShare = [AnyObject]()
if let shareTextObj = shareText{
objectsToShare.append(shareTextObj)
}
if let shareImageObj = shareImage{
objectsToShare.append(shareImageObj)
}
if shareText != nil || shareImage != nil{
let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
present(activityViewController, animated: true, completion: nil)
}else{
print("There is nothing to share")
}
}
How to Use
let img: UIImage = UIImage() // Replace this with your image from your URL
saveImage(image: img, filename: "newImage.igo") //This is where you change your extension name
let newImage: UIImage = loadImageFromName(name: "newImage.igo") //Retrieve your image with the correct extension
share(shareText: "Image going to Instagram", shareImage: newImage) //Present Activity VC.
And as DFD pointed out, you can exclude certain items from the share VC to only allow what you need.

In my navigation controller, I have a UIBarButtonItem set to System item "Action". I then created the following IBAction code:
#IBAction func shareImage(_ sender: UIBarButtonItem) {
let context = CIContext()
let final = context.createCGImage(imgFinished, from: imgFinished.extent)
let shareImage = UIImage(cgImage: final!)
let vc = UIActivityViewController(activityItems: [shareImage], applicationActivities: [])
vc.excludedActivityTypes = [
UIActivityType.airDrop,
UIActivityType.assignToContact,
UIActivityType.addToReadingList,
//UIActivityType.copyToPasteboard,
//UIActivityType.mail,
//UIActivityType.message,
//UIActivityType.openInIBooks,
//UIActivityType.postToFacebook,
//UIActivityType.postToFlickr,
UIActivityType.postToTencentWeibo,
//UIActivityType.postToTwitter,
UIActivityType.postToVimeo,
UIActivityType.postToWeibo,
UIActivityType.print,
//UIActivityType.saveToCameraRoll
]
present(vc,
animated: true,
completion: nil)
vc.popoverPresentationController?.sourceView = self.view
vc.completionWithItemsHandler = {(activity, success, items, error) in
}
}
The excludedActivityTypes list is a complete/exhaustive list of available UIActivityTypes gleaned from code-complete or command-clicking on UIActivity type and going through the resulting struct. I uncomment those I wish to exclude - but leave them in for quick reference in the future. This will give you the popup you want.

Related

Share Image Via URL

I am creating Images app from my WordPress website with json and i am using swift, i want to share image on Social Networks from my app , currently i tried this code it works but only with image name i want to share image from image url, is that possible ?
this is my code
let myWebsite = NSURL(string: "nice")
let img: UIImage = UIImage(named:"splash")!
guard let url = myWebsite else {
print("nothing found")
return
}
let shareItems:Array = [img,url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.print, UIActivity.ActivityType.postToWeibo, UIActivity.ActivityType.copyToPasteboard, UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.postToVimeo]
self!.present(activityViewController, animated: true, completion: nil)
If you need to download an image and then share you should do that separately, there is no single method that does that for you. Here is how:
func shareImageFromUrl(_ string: String) {
guard let myUrl = URL(string: string) else {
print("Invalid url!")
return
}
URLSession.shared.dataTask(with: myUrl) { (data, _, _) in
guard let data = data,
let image = UIImage(data: data) else
{ return }
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivity.ActivityType.print, UIActivity.ActivityType.postToWeibo, UIActivity.ActivityType.copyToPasteboard, UIActivity.ActivityType.addToReadingList, UIActivity.ActivityType.postToVimeo]
DispatchQueue.main.async {
self.present(activityViewController, animated: true, completion: nil)
}
}.resume()
}

how to share QR created image swift 4

I am creating QR code in swift and assigning it to an imageView
when I try to share that image with generated code, it does not shares that image,
func createCode()
{
let text = email
let data = text.data(using: .ascii, allowLossyConversion: false)
fillter = CIFilter(name: "CIQRCodeGenerator")
fillter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 5.0, y: 5.0)
CreatedImage = UIImage(ciImage: (fillter.outputImage?.transformed(by: transform))!)
imageCode.image = CreatedImage as UIImage
}
and this is share button
#IBAction func shareButtonPressed(_ sender: Any)
{
let activityItem: [UIImage] = [imageCode.image!]
let activity = UIActivityViewController(activityItems: activityItem as [UIImage], applicationActivities: [])
activity.popoverPresentationController?.sourceView = self.view
self.present(activity, animated: true, completion: nil)
}
it shows like it has nothing to share, it does not pick any bit of image
Have you created a variable to store the image somewhere e.g.
var generatedImage: UIImage?
Assuming then, that I have read your question correctly, in your creation function you can cast the image at the end of the function e.g:
generatedImage = imageCode.image
Then in your share function you could say:
guard let validQR = generatedImage else { return }
let activityItem: [UIImage] = [validQR]
let activity = UIActivityViewController(activityItems: activityItem as [UIImage], applicationActivities: [])
activity.popoverPresentationController?.sourceView = self.view
self.present(activity, animated: true, completion: nil)
I tested with an image from my Bundle e.g:
generatedImage = UIImage(named: "SCNPyramid")
And I was able to share the image :)
after searching all..
I cam I to know that make a programmatically screen shot of desired view, that is sent..
I have been having the same problem and solved it by first saving the generated qr code image to a file and then sharing the file url.
private func shareQRCode() {
guard let qrcode = self.qrCodeImage,
let data = qrcode.pngData(),
let url = self.saveInCache(data: data, fileName: "QRCode.png") else { return }
// set up activity view controller
let imageToShare = [url]
let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// work around to prevent dismissing current view after saving image
let tempController = TransparentViewController()
tempController.modalPresentationStyle = .overFullScreen
activityViewController.completionWithItemsHandler = { [weak tempController] _, _, _, _ in
if let presentingViewController = tempController?.presentingViewController {
presentingViewController.dismiss(animated: false, completion: nil)
} else {
tempController?.dismiss(animated: false, completion: nil)
}
}
present(tempController, animated: true) { [weak tempController] in
tempController?.present(activityViewController, animated: true, completion: nil)
}
}
Here is the code for saveInCache function:
private func saveInCache(data: Data, fileName: String) -> URL? {
let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
let path = paths[0]
let fileUrl = path.appendingPathComponent(fileName)
let fileManager = FileManager.default
if self.pathExists(fileUrl) {
do {
try fileManager.removeItem(at: fileUrl)
} catch { return fileUrl }
}
guard fileManager.createFile(atPath: fileUrl.path, contents: data, attributes: nil) else {
return nil
}
return fileUrl
}
private func pathExists(_ path: URL) -> Bool {
let fileManager = FileManager.default
var isDir: ObjCBool = false
if fileManager.fileExists(atPath: path.path, isDirectory: &isDir) {
if isDir.boolValue {
// file exists and is a directory
return true
} else {
// file exists and is not a directory
return true
}
} else {
// file does not exist
return false
}
}
And here a simple Transparent View Controller for ActivityViewController work around:
final class TransparentViewController: UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .clear
}
}

UIActivityViewController and UIImage preview

I'm trying to share an images using UIActivityViewController (on iMessage, Telegram, WhatsApp and other).
I'm doing:
let image = ... // an image
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
But the image is cropped very badly. I've also tried to resize the image before the user share action, using UIActivityItemSource, but the result change depending the user device and the image
How can I achieve a perfect preview on all device and apps? Any advice or reference?
Here an example of the final result:
The first image is the image shared as Sticker in iMessage and the second using UIActivityViewController
Try to compress image before use UIActivityViewController
var compressedImage: Data? = UIImageJPEGRepresentation(yourImage, 0.8)
var docsPath: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as? String ?? ""
var imagePath: String = URL(fileURLWithPath: docsPath).appendingPathComponent("image.jpg").absoluteString
var imageUrl = URL.init(fileURLWithPath: imagePath)
do {
try compressedImage?.write(to: imageUrl, options: .atomic)
} catch {
print(error)
}
// save the file
var activityViewController = UIActivityViewController(activityItems: ["Check this image", imageUrl], applicationActivities: nil)

Download image from String URL

I have the following button that receives data from a collection view cell and shares a String URL.
However I'm looking for a means to download the actual image from the URL before sharing. How can this be done in Swift?
//get buttonViewShareAction and share image
#IBAction func buttonViewShareAction(sender: UIButton) {
print("buttonViewShareAction tapped")
let face = self.faces[sender.tag]
if let imageURL = NSURL(string:face.image) {
print(imageURL)
}
let shareItems:Array = [face.image]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
}
After getting the image URL, you want to create an NSData out of it then create a UIImage from this data.
Swift 3.0
let imageData: Data = try! Data(contentsOf: URL(string: imageURL)!)
let profileImage = UIImage(data: imageData)!
Swift 2
let imageData: NSData = NSData(contentsOfURL: NSURL(string: imageURL)!)!
let profileImage = UIImage(data: imageData)!

How do I share files using share sheet in iOS?

I want to share some files I have locally in my app using Share Sheet functionality on iPhone. I display the file in a UIWebView and when the user clicks the share sheet, I want to show options (email, WhatsApp, etc. ) to share the file displayed on the UIWebView. I know that we can use
func displayShareSheet(shareContent:String) {
let activityViewController = UIActivityViewController(activityItems: [shareContent as NSString], applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: {})
}
to share a string for example. How do I change this code to share documents?
Swift 4.2 and Swift 5
If you already have a file in a directory and want to share it, just add it's URL into activityItems:
let fileURL = NSURL(fileURLWithPath: "The path where the file you want to share is located")
// Create the Array which includes the files you want to share
var filesToShare = [Any]()
// Add the path of the file to the Array
filesToShare.append(fileURL)
// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
// Show the share-view
self.present(activityViewController, animated: true, completion: nil)
If you need to make the file:
I'm using this extension to make files from Data (read the comments in the code for explanation how it works):
As in the typedef's answer, get the current documents directory:
/// Get the current directory
///
/// - Returns: the Current directory in NSURL
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory as NSString
}
Extension for Data:
extension Data {
/// Data into file
///
/// - Parameters:
/// - fileName: the Name of the file you want to write
/// - Returns: Returns the URL where the new file is located in NSURL
func dataToFile(fileName: String) -> NSURL? {
// Make a constant from the data
let data = self
// Make the file path (with the filename) where the file will be loacated after it is created
let filePath = getDocumentsDirectory().appendingPathComponent(fileName)
do {
// Write the file from data into the filepath (if there will be an error, the code jumps to the catch block below)
try data.write(to: URL(fileURLWithPath: filePath))
// Returns the URL where the new file is located in NSURL
return NSURL(fileURLWithPath: filePath)
} catch {
// Prints the localized description of the error from the do block
print("Error writing the file: \(error.localizedDescription)")
}
// Returns nil if there was an error in the do-catch -block
return nil
}
}
Examples how to use:
Share image-files:
// Your image
let yourImage = UIImage()
in png-file
// Convert the image into png image data
let pngImageData = yourImage.pngData()
// Write the png image into a filepath and return the filepath in NSURL
let pngImageURL = pngImageData?.dataToFile(fileName: "nameOfYourImageFile.png")
// Create the Array which includes the files you want to share
var filesToShare = [Any]()
// Add the path of png image to the Array
filesToShare.append(pngImageURL!)
// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
// Show the share-view
self.present(activityViewController, animated: true, completion: nil)
in jpg-file
// Convert the image into jpeg image data. compressionQuality is the quality-compression ratio in % (from 0.0 (0%) to 1.0 (100%)); 1 is the best quality but have bigger filesize
let jpgImageData = yourImage.jpegData(compressionQuality: 1.0)
// Write the jpg image into a filepath and return the filepath in NSURL
let jpgImageURL = jpgImageData?.dataToFile(fileName: "nameOfYourImageFile.jpg")
// Create the Array which includes the files you want to share
var filesToShare = [Any]()
// Add the path of jpg image to the Array
filesToShare.append(jpgImageURL!)
// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
// Show the share-view
self.present(activityViewController, animated: true, completion: nil)
Share text-files:
// Your String including the text you want share in a file
let text = "yourText"
// Convert the String into Data
let textData = text.data(using: .utf8)
// Write the text into a filepath and return the filepath in NSURL
// Specify the file type you want the file be by changing the end of the filename (.txt, .json, .pdf...)
let textURL = textData?.dataToFile(fileName: "nameOfYourFile.txt")
// Create the Array which includes the files you want to share
var filesToShare = [Any]()
// Add the path of the text file to the Array
filesToShare.append(textURL!)
// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
// Show the share-view
self.present(activityViewController, animated: true, completion: nil)
Other files:
You can make a file from anything which is in Data format and as far as I know, almost everything in Swift can be converted into Data like String, Int, Double, Any...:
// the Data you want to share as a file
let data = Data()
// Write the data into a filepath and return the filepath in NSURL
// Change the file-extension to specify the filetype (.txt, .json, .pdf, .png, .jpg, .tiff...)
let fileURL = data.dataToFile(fileName: "nameOfYourFile.extension")
// Create the Array which includes the files you want to share
var filesToShare = [Any]()
// Add the path of the file to the Array
filesToShare.append(fileURL!)
// Make the activityViewContoller which shows the share-view
let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)
// Show the share-view
self.present(activityViewController, animated: true, completion: nil)
I want to share my solution of UIActivityViewController and sharing text as a image file. This solution works for sharing via Mail and even Save to Dropbox.
#IBAction func shareCsv(sender: AnyObject) {
//Your CSV text
let str = self.descriptionText.text!
filename = getDocumentsDirectory().stringByAppendingPathComponent("file.png")
do {
try str.writeToFile(filename!, atomically: true, encoding: NSUTF8StringEncoding)
let fileURL = NSURL(fileURLWithPath: filename!)
let objectsToShare = [fileURL]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
} catch {
print("cannot write file")
// failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding
}
}
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
Here's the Swift 3 version:
let dictToSave: [String: Any] = [
"someKey": "someValue"
]
let jsonData = try JSONSerialization.data(withJSONObject: dictToSave, options: .prettyPrinted)
let filename = "\(self.getDocumentsDirectory())/filename.extension"
let fileURL = URL(fileURLWithPath: filename)
try jsonData.write(to: fileURL, options: .atomic)
let vc = UIActivityViewController(activityItems: [fileURL], applicationActivities: [])
self.present(vc, animated: true)
func getDocumentsDirectory() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}

Resources