PHAssetResource.assetResources(for: asset).first.OriginalFilename 在 300 个资产后给出 nil [英] PHAssetResource.assetResources(for: asset).first.OriginalFilename gives nil after 300 assets

查看:47
本文介绍了PHAssetResource.assetResources(for: asset).first.OriginalFilename 在 300 个资产后给出 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

extension PHAsset {

    var originalFilename: String? {

        var fname:String?

            let resources = PHAssetResource.assetResources(for: self)
            if let resource = resources.first {
                fname = resource.originalFilename
            }
        return fname
    }
}

我正在使用 PHAsset 的这个扩展来获取资产的原始文件名.大约有 2000 个资产,此扩展名给出前 300 个资产的原始文件名,之后返回 nil.我很困惑,这怎么可能?Xcode 中是否有任何错误?但是我使用的是 Xcode 8.1 和 swift 3.0.谁能帮我解决这个问题?提前致谢.

I am using this extension of PHAsset to get the Original file name of asset. There are approximately 2000 assets, this extension gives the original file name of first 300 assets and after that it returns nil. I'm confused that how is it possible? Is there any bug in Xcode? however I'm using Xcode 8.1 and swift 3.0. Can anyone help me out to solve this issue? Thanks in advance.

推荐答案

这看起来像我曾经遇到的一个错误:PHAsset assetResourcesForAsset 调用过于频繁时失败

This looks like a bug I once had: PHAsset assetResourcesForAsset fails when called too often

事实证明这是一个糟糕的内存管理问题.在某些时候,设备的可用 RAM 非常低,这就是它开始返回 nil 的时候.通过将周围的操作放在 autorelease 块中(我正在加载 PHAsset),问题就消失了.

It turned out to be a bad memory management issue. At some point, the free RAM of the device was very low, and that's when it started to return nil. By putting the surrounding operation in an autorelease block (where I was loading the PHAsset), the problem went away.

我仍然有一些罕见的情况,原始文件名是 nil(或者资产没有资源,我真的不知道),但这可能来自资产中的错误).

I still have some rare cases where the original filename is nil (or the asset has no resource, I don't really know), but this could maybe come from a bug in the asset).

这篇关于PHAssetResource.assetResources(for: asset).first.OriginalFilename 在 300 个资产后给出 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆