【问题标题】:What is the file size limit in temporary directory in iOS?iOS中临时目录的文件大小限制是多少?
【发布时间】:2019-05-09 06:39:54
【问题描述】:

我正在尝试保存从here 下载的大小为 7.9MB 的图像。但是在'try data.write ...'行,扩展程序崩溃了,我在控制台中得到了这个。

内核 EXC_RESOURCE -> 通知扩展[3137] 超出内存限制:ActiveHard 12 MB(致命)

内核 46710.034 内存状态:killing_specific_process pid 3137 [通知扩展](每个进程限制 3)-memorystatus_available_pages:73906

ReportCrash 启动延长事务计时器 默认 18:39:53.104640 +0530

ReportCrash 进程通知扩展 [3137] 被 jetsam 原因每个进程限制杀死

是不是因为 7.9MB 的大小太大而无法处理。如果是,那么它没有意义,因为在创建 UNNotificationAttachment 对象之前需要将媒体保存在临时存储中。在官方文档中,png 文件的限制为 10 MB,视频为 50 MB。我该如何解决?

let fileManager = FileManager.default
let folderName = ProcessInfo.processInfo.globallyUniqueString

guard let folderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(folderName, isDirectory: true) else {
        return nil
}

do {
    try fileManager.createDirectory(at: folderURL, withIntermediateDirectories: true, attributes: nil)
    let fileURL = folderURL.appendingPathComponent(fileIdentifier)
    try data.write(to: fileURL, options: [])
    let attachment = try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL, options: options)
        return attachment
} catch let error {

}

【问题讨论】:

  • 看起来是内存问题而不是存储空间问题。
  • 你的图片压缩了吗?如果您的数据是 jpeg 并且在过程中的某个位置被解压缩到 UIImage,它可能会超过 12MB

标签: ios swift ios-extensions unnotificationattachment unnotificationserviceextension


【解决方案1】:

我不知道您可以写入临时目录(可用空间除外)的内容是否大小限制,但我可以告诉您,我已经编写了客户端应用程序将数百兆字节写入该目录。那不是你的问题。

【讨论】:

  • 限制是当前可用空间,如果超过catch会抛出错误
  • 您是密切关注tmp 文件夹的大小,还是在它变得太大时让操作系统清除它?
【解决方案2】:

它是这样工作的。我没有使用NSData(contentsOf:) 下载数据,而是使用URLSession.shared.downloadTask(with:)。这会自行存储下载的数据,因此我们无需编写它。只需使用FileManager.shared.moveItem(at:to:) 将其移动到所需的临时位置。看起来问题出在NSData.write(to:options:)function 上。它有一些大小限制。

URLSession.shared.downloadTask(with: url) { (location, response, error) in

    if let location = location {
        let tmpDirectory = NSTemporaryDirectory()
        let tmpFile = "file://".appending(tmpDirectory).appending(url.lastPathComponent)
        let tmpUrl = URL(string: tmpFile)!
        try! FileManager.default.moveItem(at: location, to: tmpUrl)
        if let attachment = try? UNNotificationAttachment(identifier: "notification-img", url: tmpUrl) {
        bestAttemptContent.attachments = [attachment]
        }
    }
    contentHandler(bestAttemptContent)
}.resume()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2011-07-26
    • 2013-04-08
    • 2011-03-19
    • 2013-01-04
    相关资源
    最近更新 更多