gpt4 book ai didi

ios - 来自本地通知附件的图像未显示在 UNNotificationContentExtension 中

转载 作者:IT王子 更新时间:2023-10-29 05:23:08 24 4
gpt4 key购买 nike

我一直致力于在 iOS10 中引入丰富的通知体验,并坚持将图像作为附件传递给 UNNotificationContentExtension .

这是我的 ContentExtension:

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet weak var attachmentImage: UIImageView!

func didReceive(_ notification: UNNotification) {

if let attachment = notification.request.content.attachments.first {
if attachment.url.startAccessingSecurityScopedResource() {
attachmentImage.image = UIImage(contentsOfFile: attachment.url.path)
attachment.url.stopAccessingSecurityScopedResource()
}
}
}
}

作为教程,我一直在关注Advanced Notifications video from WWDC .我检查过 - UIImage我正在分配给 UIImageView:

  • 不是 nil
  • 有适当的CGSize (191x191)
  • attachment.url.path 等于 /var/mobile/Library/SpringBoard/PushStore/Attachments/<bundle of app>/<...>.png

以下是我从应用程序发送本地通知的方式:

    let content = UNMutableNotificationContent()
content.title = "Sample title"
content.body = "Sample body"
content.categoryIdentifier = "myNotificationCategory"

let attachement = try! UNNotificationAttachment(identifier: "image",
url: Bundle.main.url(forResource: "cat", withExtension: "png")!,
options: nil)

content.attachments = [ attachement ]

let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: nil)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
}
}

“cat.png”只是我添加到项目中的虚拟资源。 enter image description here

如您所见,通知显示图片,所以我假设我发送正确,但在展开状态(在 NotificationViewController 中)我从未成功显示相同的图片。

我做错了什么?谢谢!

最佳答案

当您使用 contentsOfFile 创建 UIImage 时,UIImage 对象仅读取图像 header ,它指示基本信息,例如图像大小等。

因此,尝试将 stopAccessingSecurityScopedResource 移动到 [NotificationViewController dealloc]

或使用以下内容:

  • objective-c代码:

    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];
  • swift代码:

    let imageData = NSData(contentsOf: attachment.url)
    let image = UIImage(data: imageData! as Data)

没有文档说contentsOfFile 只读图片头。但是当我运行以下代码时:

NSString *docFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *pngPath = [docFolderPath stringByAppendingPathComponent:@"test.png"];
UIImage *image = [UIImage imageWithContentsOfFile:pngPath];
[[NSFileManager defaultManager] removeItemAtPath:pngPath error:nil];
imageView.image = image;

发生错误:

ImageIO: createDataWithMappedFile:1322:  'open' failed '/Users/fanjie/Library/Developer/CoreSimulator/Devices/FFDFCA06-A75E-4B54-9FC2-8E2AAE3B1405/data/Containers/Data/Application/E2D26210-4A53-424E-9FE8-D522CFD4FD9E/Documents/test.png'
error = 2 (No such file or directory)

所以我得出结论,UIImage contentsOfFile 只读取图片头。

关于ios - 来自本地通知附件的图像未显示在 UNNotificationContentExtension 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063942/

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