用這個看起來沒有關注的簡單問題獲得巨大的賞金。獲取保存到照片相冊的圖像的文件名
在現代的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'屬性嗎? –