簡體   English   中英

UIActivityViewController 共享 SVG 文件

[英]UIActivityViewController share SVG file

我嘗試與 UIActivityViewController 共享 SVG 文件,但只有消息和郵件可用選項。 如果我選擇郵件,則沒有附加任何內容。 有沒有辦法做到這一點?

您應該將您的字符串作為數據保存到應用程序臨時或文檔文件夾中,然后使用 url 作為活動項,操作系統可以很好地處理它:

extension Data {
/// Data into file
///
/// - Parameters:
///   - fileName: the Name of the file you want to write, remember to include extension.
/// - Returns: Returns the URL where the new file is located in
func toFile(fileName: String) -> URL? {
    var filePath: URL = URL(fileURLWithPath: NSTemporaryDirectory())
    filePath.appendPathComponent(fileName)

    do {
        try write(to: filePath)
        return filePath
    } catch {
        print("Error writing the file: \(error.localizedDescription)")
    }
    return nil
}

}

然后您可以執行以下操作:

let textData = Data(yourSVGString.utf8)
let uniqueRandomName = String(UUID().uuidString.suffix(4))
let svgURL = textData.toFile(fileName: "\(uniqueRandomName).svg")
let ac = UIActivityViewController(activityItems: [svgURL], applicationActivities: nil)
self.present(ac, animated: true, completion: nil)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM