gpt4 book ai didi

ios - 如何使用 Assets 库获取 UIImagePickerController 中的文件名?

转载 作者:搜寻专家 更新时间:2023-10-30 21:47:48 25 4
gpt4 key购买 nike

我想从 UIImagePickerController 获取文件名。我不想使用 ALAssetLibrary,因为它在 iOS 9 中已被弃用。我使用了以下代码,但它始终将每个文件的图像名称返回为“Asset.jpg”。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {       
let originalImage = (info[UIImagePickerControllerOriginalImage] as? UIImage)!
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
imageData = UIImageJPEGRepresentation(originalImage, 100) as NSData?
let data = UploadData()
data.fileName = url.lastPathComponent
picker.dismiss(animated: true, completion: nil)
}

最佳答案

import Photos

iOS 11 之前

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
let assetResources = PHAssetResource.assetResources(for: result.firstObject!)

print(assetResources.first!.originalFilename)
}

dismiss(animated: true, completion: nil)
}

iOS 11 之后

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = info[UIImagePickerController.InfoKey.phAsset.rawValue] as? PHAsset {
let assetResources = PHAssetResource.assetResources(for: asset)
print(assetResources.first!.originalFilename)
}
dismiss(animated: true, completion: nil)
}

关于ios - 如何使用 Assets 库获取 UIImagePickerController 中的文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40627901/

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