我想在给定图像URL的情况下将图像附加到我的本地通知中.这是创建附件的扩展:
import UserNotifications extension UNNotificationAttachment { static func create(identifier: String,image: UIImage,options: [NSObject : AnyObject]?) -> UNNotificationAttachment? { let fileManager = FileManager.default let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString let tmpSubFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName,isDirectory: true) do { try fileManager.createDirectory(at: tmpSubFolderURL,withIntermediateDirectories: true,attributes: nil) let imageFileIdentifier = identifier+".png" let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier) guard let imageData = UIImagePNGRepresentation(image) else { return nil } try imageData.write(to: fileURL) let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier,url: fileURL,options: options) return imageAttachment } catch { print("error " + error.localizedDescription) } return nil } }
当我安排新通知时,我会像这样使用它:
// url of the image such as http://www.unsplash.com/image.png let data = try? Data(contentsOf: url) guard let myImage = UIImage(data: data!) else { return } if let attachment = UNNotificationAttachment.create(identifier: key,image: myImage,options: nil) { content.attachments = [attachment] }
创建这样的通知会冻结应用程序几秒钟,因为应用程序会同步下载图像.我也试过使用DispatchQueue,但它没有改变任何东西.我做错了什么?
解决方法
您的代码下载图像,解析它以创建UIImage,将图像转换回PNG数据块,然后将此数据写入临时文件.
您可以跳过创建UIImage的步骤并将其转换回文件.
尝试使用URLSession和URLDataTask:
let fileURL = ... let task = URLSession.shared.dataTask(with: url) { (data,_,_) in do { try imageData.write(to: fileURL) let attachment = UNNotificationAttachment.create(identifier: key,options: nil) // call closure to call back with attachment and/or error } catch let ex { // call closure with error } } task.resume()
我遗漏了一些错误处理和其他细节,但这应该让你大致了解异步执行它需要什么. URLSessions使用GCD执行异步网络.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。