gpt4 book ai didi

ios - 获取保存到相册的图片文件名

转载 作者:技术小花猫 更新时间:2023-10-29 11:05:41 47 4
gpt4 key购买 nike

在现代 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") }
}

问题在于它无法在赛道条件下运行。

这非常笨拙,而且在很多方面似乎都令人担忧。

今天真的是这样吗?

最佳答案

extension PHPhotoLibrary {

func save(imageData: Data, withLocation location: CLLocation?) -> Promise<PHAsset> {
var placeholder: PHObjectPlaceholder!
return Promise { fullfil, reject in
performChanges({
let request = PHAssetCreationRequest.forAsset()
request.addResource(with: .photo, data: imageData, options: .none)
request.location = location
placeholder = request.placeholderForCreatedAsset
}, completionHandler: { (success, error) -> Void in
if let error = error {
reject(error)
return
}

guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: .none).firstObject else {
reject(NSError())
return
}

fullfil(asset)
})
}
}
}

我认为您可以使用 PHPhotoLibraryPHObjectPlaceholder 来做到这一点。

关于ios - 获取保存到相册的图片文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43791151/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com