我正在使用UIActivityViewController共享内容,但是共享的文件名不同(随机命名)。当我与Photos应用程序分享时,它会使用真实名称共享图像。我的问题是:如何使用UIActivityViewController共享带有其名称的图像?
回答: 大家好, 是的,我得到了答案。 首先,在docuementDirectory中保存图像,然后共享图像链接。请确保共享图像链接(在docuement目录中保存的图像)而不是图像本身。
@IBAction func shareImage(_ sender: UIButton) {
回答: 大家好, 是的,我得到了答案。 首先,在docuementDirectory中保存图像,然后共享图像链接。请确保共享图像链接(在docuement目录中保存的图像)而不是图像本身。
@IBAction func shareImage(_ sender: UIButton) {
var selectedImageArrayToShare:Array<NSURL> = [NSURL]()
selectedImageArrayToShare.append(saveActivityControllerImage())
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: selectedImageArrayToShare as [Any], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}
func saveActivityControllerImage() -> NSURL {
var name:String=selectedPhotoName!.capitalized
print(name.components(separatedBy: ".").last as Any)
if name.components(separatedBy: ".").last!.lowercased() != "png" {
name = name.replacingOccurrences(of: ".\(name.components(separatedBy: ".").last!)", with: ".jpg")
}
else {
name = name.replacingOccurrences(of: ".\(name.components(separatedBy: ".").last!)", with: ".png")
}
let urlString : NSURL = getDocumentDirectoryPath(fileName: name)
print("Image path : \(urlString)")
if !FileManager.default.fileExists(atPath: urlString.absoluteString!) {
do {
var isSaved : Bool = false
print(urlString.pathExtension as Any)
if urlString.pathExtension?.lowercased() == "png" {
isSaved = ((try UIImagePNGRepresentation(photoPreviewImageView.image!)?.write(to: urlString as URL, options: Data.WritingOptions.atomic)) != nil)
}
else {
isSaved = ((try UIImageJPEGRepresentation(photoPreviewImageView.image!, 1.0)?.write(to: urlString as URL, options: Data.WritingOptions.atomic)) != nil)
}
if (isSaved) {
return urlString
} else {
return NSURL.fileURL(withPath: "Blank") as NSURL
}
} catch {
return NSURL.fileURL(withPath: "Blank") as NSURL
}
}
return urlString
}
函数getDocumentDirectoryPath(fileName:String) -> 返回NSURL类型的路径。
let paths:NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
let docuementDir:NSString = paths.object(at: 0) as! NSString
return NSURL.fileURL(withPath: docuementDir.appendingPathComponent("NameIt/\(fileName)")) as NSURL
}