gpt4 book ai didi

ios - 使用 UIActivityViewController 共享图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:09:16 26 4
gpt4 key购买 nike

<分区>

我正在与 UIActivityViewController 共享,但它是用不同的文件名(随机名称)共享的。当我与照片应用程序共享时,它会使用真实姓名共享图像。我的问题是:如何使用 UIActivityViewController 分享带有名字的图片?

答案:大家好,

是的,我得到了答案。首先将图像保存在 docuementDirectory 中,然后共享图像链接。请确保共享图片链接(保存在文档目录中的图片)而不是图片。

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

func 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
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com