【问题标题】:Image from attachment from Local notification is not shown in UNNotificationContentExtension本地通知附件中的图像未显示在 UNNotificationContentExtension 中
【发布时间】:2016-12-28 02:01:52
【问题描述】:

我一直致力于开发 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 的一个虚拟资源。

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

我做错了什么? 谢谢!

【问题讨论】:

  • 您好,我仍然面临无法在展开状态下显示图像的问题。你能帮帮我吗?我的代码在目标 c 中。

标签: ios swift ios10 unnotificationattachment


【解决方案1】:

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

所以,请尝试将 stopAccessingSecurityScopedResource 移至 [NotificationViewController dealloc]

或使用以下:

  • objective-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)

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

【讨论】:

  • 杰弗里,你是我的英雄。很棒的收获和解释!稍微修改了答案以添加我使用的 swift-version。现在就像一个魅力!
  • @Jeffery 你在哪里读到contentsOfFile 只加载图像标题?这是来自文档的讨论:“此方法将图像数据加载到内存中并将其标记为可清除。如果数据被清除并需要重新加载,则图像对象会从指定路径再次加载该数据。”我认为问题可能是您正在调用 stopAccessingSecurityScopedResource 清除图像内存并且它关闭了对图像文件的访问并且无法重新加载。
  • @Kostiantyn Koval 对不起。没有文档说contentsOfFile 只加载图像标题。我认为调用stopAccessingSecurityScopedResource 不会清除图像内存。在评论中发布代码并不能令人信服,所以我在答案中添加了一些解释。
  • 啊,这是附加代码示例。是的imageWithContentsOfFile 需要文件存在,因为它会延迟加载它的数据,所以当你删除文件或通过调用 stopAccessingSecurityScopedResource 关闭对它的访问时,你会得到一个错误。
  • @Jeffery 嗨,我也面临同样的问题,我无法在展开状态下显示图像。你能帮帮我吗?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2016-10-26
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多