【发布时间】:2017-05-04 19:19:21
【问题描述】:
在现代 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") }
}
问题在于它在赛道条件下失败。
这非常笨拙,而且在很多方面看起来都令人担忧。
今天真的要走吗?
【问题讨论】:
-
你真的需要这个网址吗?是否也可以使用相关
PHObject的localIdentifier属性?