Reputation: 137
extension PHAsset {
var originalFilename: String? {
var fname:String?
let resources = PHAssetResource.assetResources(for: self)
if let resource = resources.first {
fname = resource.originalFilename
}
return fname
}
}
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.
Upvotes: 1
Views: 1921
Reputation: 317
This looks like a bug I once had: PHAsset assetResourcesForAsset fails when called too often
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.
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).
Upvotes: 1
Reputation: 6293
It seems like the originalFilename is missing in the iOS Photo Library for some of your assets. This can happen for different reasons (bug, certain assets were restored from iCloud). You should handle this case in your code and e.g. generate an own filename (e.g. an UUID based one), when originalFilename is missing.
Upvotes: 0