用这个看起来没有关注的简单问题获得巨大的赏金。获取保存到照片相册的图像的文件名
在现代的iOS(2017),
这里其实是我知道只有这样,才能将图像保存到了iOS系统中的照片,并获得文件名/路径。
import UIKit
import Photos
func saveTheImage...() {
UIImageWriteToSavedPhotosAlbum(yourUIImage, self,
#selector(Images.image(_:didFinishSavingWithError:contextInfo:)),
nil)
}
func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
guard error == nil else {
print("Couldn't save the image!")
return
}
doGetFileName()
}
func doGetFileName() {
let fo: PHFetchOptions = PHFetchOptions()
fo.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let r = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fo)
if let mostRecentThingy = r.firstObject {
PHImageManager.default().requestImageData(
for: mostRecentThingy,
options: PHImageRequestOptions(),
resultHandler: { (imagedata, dataUTI, orientation, info) in
if info!.keys.contains("PHImageFileURLKey") {
let path = info!["PHImageFileURLKey"] as! NSURL
print("Holy cow. The path is \(path)")
}
else { print("bizarre problem") }
})
}
else { print("unimaginable catastrophe") }
}
有两个问题:
1. WTH?!?
2.它在赛道条件下失败。
这是惊人的笨拙,它似乎令人担忧的方式有很多。
今天真的要走吗?
你真的需要这个网址吗?你还可以使用相关的'PHObject'的'localIdentifier'属性吗? –