栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

具有UIImage或远程URL的UNNotificationAttachment

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具有UIImage或远程URL的UNNotificationAttachment

  1. 在tmp文件夹中创建目录
  2. 将的
    NSData
    表示形式写入
    UIImage
    新创建的目录
  3. 使用URL到tmp文件夹中的文件创建UNNotificationAttachment
  4. 清理tmp文件夹

我写了一个扩展

UINotificationAttachment

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) let imageData = UIImage.pngData(image) 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    }}

因此,

UNUserNotificationRequest
使用
UNUserNotificationAttachment
a
进行创建
UIImage
就可以像这样

let identifier = ProcessInfo.processInfo.globallyUniqueStringlet content = UNMutableNotificationContent()content.title = "Hello"content.body = "World"if let attachment = UNNotificationAttachment.create(identifier: identifier, image: myImage, options: nil) {    // where myImage is any UIImage    content.attachments = [attachment] }let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120.0, repeats: false)let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)UNUserNotificationCenter.current().add(request) { (error) in    // handle error}

这应该可行,因为

UNNotificationAttachment
会将图像文件复制到自己的位置。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636372.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号