14

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

这是我的内容扩展:

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()
            }
        }
    }
}

作为教程,我一直在关注WWDC 的 Advanced Notifications 视频。我已经检查过 -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 的一个虚拟资源。 在此处输入图像描述

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

我究竟做错了什么?谢谢!

4

2 回答 2

25

当你用 来创建 UIImage 时contentsOfFile,UIImage 对象只读取图像头,它指示基本信息,例如图像大小等。

所以,尝试移动stopAccessingSecurityScopedResource[NotificationViewController dealloc].

或使用以下:

  • 目标c代码:

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

    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)

所以我得出一个结论,UIImagecontentsOfFile只读取图片头。

于 2016-08-22T12:56:57.400 回答
1

感谢@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()
            }
        }
于 2019-11-28T08:59:05.243 回答