Difference between revisions of "Using PHAsset"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (Created page with " * https://stackoverflow.com/questions/27854937/ios8-photos-framework-how-to-get-the-nameor-filename-of-a-phasset <pre> let fetchResult = PHAsset.fetchAssets(with: .image, o...") |
PeterHarding (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 20: | Line 20: | ||
} | } | ||
} | } | ||
</pre> | |||
<pre> | |||
extension PHAsset { | |||
var originalFilename: String? { | |||
var fname:String? | |||
if #available(iOS 9.0, *) { | |||
let resources = PHAssetResource.assetResources(for: self) | |||
if let resource = resources.first { | |||
fname = resource.originalFilename | |||
} | |||
} | |||
if fname == nil { | |||
// this is an undocumented workaround that works as of iOS 9.1 | |||
fname = self.value(forKey: "filename") as? String | |||
} | |||
return fname | |||
} | |||
} | |||
</pre> | |||
<pre> | |||
extension PHAsset { | |||
var originalFilename: String? { | |||
return PHAssetResource.assetResources(for: self).first?.originalFilename | |||
} | |||
} | |||
</pre> | |||
<pre> | |||
if let asset = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl], options: nil).firstObject as? PHAsset { | |||
PHImageManager.defaultManager().requestImageDataForAsset(asset, options: nil, resultHandler: { _, _, _, info in | |||
if let fileName = (info?["PHImageFileURLKey"] as? NSURL)?.lastPathComponent { | |||
//do sth with file name | |||
} | |||
}) | |||
} | |||
</pre> | |||
<pre> | |||
import Photos | |||
... | |||
if let asset = PHAsset.fetchAssets(withALAssetURLs: [info[UIImagePickerControllerReferenceURL] as! URL], | |||
options: nil).firstObject { | |||
PHImageManager.default().requestImageData(for: asset, options: nil, resultHandler: { _, _, _, info in | |||
if let fileName = (info?["PHImageFileURLKey"] as? NSURL)?.lastPathComponent { | |||
print("///////" + fileName + "////////") | |||
//do sth with file name | |||
} | |||
}) | |||
} | |||
</pre> | </pre> | ||
[[Category:IOS]] | [[Category:IOS]] |
Latest revision as of 23:33, 15 February 2019
let fetchResult = PHAsset.fetchAssets(with: .image, options: nil) if fetchResult.count > 0 { if let asset = fetchResult.firstObject { let date = asset.creationDate ?? Date() print("Creation date: \(date)") PHImageManager.default().requestImageData(for: asset, options: PHImageRequestOptions(), resultHandler: { (imagedata, dataUTI, orientation, info) in if let info = info { if info.keys.contains(NSString(string: "PHImageFileURLKey")) { if let path = info[NSString(string: "PHImageFileURLKey")] as? NSURL { print(path) } } } }) } }
extension PHAsset { var originalFilename: String? { var fname:String? if #available(iOS 9.0, *) { let resources = PHAssetResource.assetResources(for: self) if let resource = resources.first { fname = resource.originalFilename } } if fname == nil { // this is an undocumented workaround that works as of iOS 9.1 fname = self.value(forKey: "filename") as? String } return fname } }
extension PHAsset { var originalFilename: String? { return PHAssetResource.assetResources(for: self).first?.originalFilename } }
if let asset = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl], options: nil).firstObject as? PHAsset { PHImageManager.defaultManager().requestImageDataForAsset(asset, options: nil, resultHandler: { _, _, _, info in if let fileName = (info?["PHImageFileURLKey"] as? NSURL)?.lastPathComponent { //do sth with file name } }) }
import Photos ... if let asset = PHAsset.fetchAssets(withALAssetURLs: [info[UIImagePickerControllerReferenceURL] as! URL], options: nil).firstObject { PHImageManager.default().requestImageData(for: asset, options: nil, resultHandler: { _, _, _, info in if let fileName = (info?["PHImageFileURLKey"] as? NSURL)?.lastPathComponent { print("///////" + fileName + "////////") //do sth with file name } }) }