gpt4 book ai didi

swift - 如何在按钮上显示图像名称?我们什么时候上传?

转载 作者:行者123 更新时间:2023-11-30 11:43:03 24 4
gpt4 key购买 nike

我正在通过单击按钮上传图像,并且我想在同一按钮上显示图像名称(我正在使用该按钮上传图像)我该怎么办?

    @IBAction func btnChooseImageAction(_ sender: Any) {

if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum {
print("Button capture")

imagePicker.delegate = self
imagePicker.sourceType = .savedPhotosAlbum;
imagePicker.allowsEditing = false

self.present(imagePicker, animated: true, completion: nil)
}
}

最佳答案

当您使用 UIImagePickerController 选择图像并上传到服务器时。请按照以下步骤获取文件名

第 1 步。
导入AssetsLibrary
导入照片

第 2 步。确保 Info.plist 中必须包含一个 NSPhotoLibraryUsageDescription 键,其中包含一个字符串值,向用户解释应用如何使用此数据。

步骤3.图像选择器 Controller 委托(delegate)方法来获取所选图像的信息

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let url = info[UIImagePickerControllerReferenceURL] as! URL
let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
let fileName = PHAssetResource.assetResources(for: assets.firstObject!).first!.originalFilename
print("fileName = \(fileName)")

dismiss(animated: true, completion: nil)

}

注意:可能会出现一些警告,您可以通过查看文档或谷歌搜索来解决。如果这个答案对您有帮助,请投票。

关于swift - 如何在按钮上显示图像名称?我们什么时候上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49124023/

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