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

时间:2016-08-21 12:01:11

标签: ios swift ios10 unnotificationattachment

我一直在研究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”只是我添加到proj的虚拟资源。 enter image description here

正如你所看到的,通知显示了pic,所以我认为,我正确地发送它,但是在扩展状态(NotificationViewController)中,我从未成功显示相同的图像。< / p>

我做错了什么? 谢谢!

2 个答案:

答案 0 :(得分:24)

使用contentsOfFile创建UIImage时,UIImage对象仅读取图像标题,指示基本信息,如图像大小等。

因此,请尝试将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只读取图像标题。

答案 1 :(得分:0)

感谢@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()
            }
        }