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

Image from attachment from Local notification is not shown in UNNotificationContentExtension

我一直在研究在 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:

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

    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" 只是我添加到项目中的虚拟资源。

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

我做错了什么? 谢谢!

当您使用 contentsOfFile 创建 UIImage 时,UIImage object 仅读取图像 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只读取图像header。但是当我 运行 以下代码时:

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 只读取图片 header.

感谢@jeffery,

以下是通知扩展中显示的图片的确切代码:

if let attachment = notification.request.content.attachments.first {
            if attachment.url.startAccessingSecurityScopedResource() {
                let data = NSData(contentsOfFile: attachment.url.path);
                self. attachmentImage?.image = UIImage(data: data! as Data);
                attachment.url.stopAccessingSecurityScopedResource()
            }
        }