如何在按钮上显示图像名称?当我们上传它?
Posted
技术标签:
【中文标题】如何在按钮上显示图像名称?当我们上传它?【英文标题】:how I show image name on button ? When we Uploading it? 【发布时间】:2018-03-06 05:35:09 【问题描述】:我正在通过单击按钮上传图像,我想在同一个按钮上显示图像名称(我用来上传图像)我该怎么办?
@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
: ***.com/questions/24625687/…
点击此链接从库中获取图像名称:***.com/questions/40627901/…
【参考方案1】:
当您使用 UIImagePickerController 选择图像并上传到服务器时。请按照以下步骤获取文件名
第 1 步。import AssetsLibrary
import Photos
第 2 步。确保 Info.plist 中必须包含一个 NSPhotoLibraryUsageDescription 键和一个字符串值,向用户解释应用程序如何使用这些数据。
Step 3. Image Picker Controller 委托方法获取所选图像的信息
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)
注意:您可以通过查看文档或谷歌搜索来解决一些警告。 如果这个答案对你有帮助,请投票。
【讨论】:
【参考方案2】:这是在按钮上获取任何图像名称的完整且正确的代码。当我们上传它时。
@IBAction func btnChooseImageAction(_ sender: Any)
let actionSheet = UIAlertController(title: "Choose From", message: "Choose an image from.", preferredStyle: .actionSheet)
let camera = UIAlertAction(title: "Camera", style: .default) (UIAlertAction) -> Void in
print("Clicked on Camera")
if UIImagePickerController.isSourceTypeAvailable(.camera)
self.picker.sourceType = .camera
//check device camera front or rear availabilty
if UIImagePickerController.isCameraDeviceAvailable(.front)
self.picker.cameraDevice = .front
//cehck for flash
if UIImagePickerController.isFlashAvailable(for: .front)
self.picker.cameraFlashMode = .on
else
self.picker.cameraDevice = .rear
if UIImagePickerController.isFlashAvailable(for: .front)
self.picker.cameraFlashMode = .on
//check what you want to capture photo or video
self.picker.cameraCaptureMode = .photo
self.picker.allowsEditing = true
self.present(self.picker, animated: true, completion: () -> Void in
)
let gallery = UIAlertAction(title: "Gallery", style: .default) (UIAlertAction) -> Void in
print("Clicked on Gallery")
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary)
self.picker.sourceType = .photoLibrary
self.picker.allowsEditing = true
self.present(self.picker, animated: true, completion: () -> Void in
)
let cancel = UIAlertAction(title: "Cancel", style: .cancel) (UIAlertAction) -> Void in
print("Clicked on Cancel")
actionSheet.addAction(gallery)
actionSheet.addAction(camera)
actionSheet.addAction(cancel)
self.present(actionSheet, animated: true) () -> Void in
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
if let image = info[UIImagePickerControllerEditedImage] as? UIImage
imageview.image = image
let sonu = getFileName(info: info)
print(sonu)
if sonu == "JPG"
imageDataForApi = UIImagePNGRepresentation(image)
else if sonu == "PNG"
imageDataForApi = UIImageJPEGRepresentation(image, 0)
self.dismiss(animated: true) () -> Void in
func getFileName(info: [String : Any]) -> String
if let assetPath = info[UIImagePickerControllerReferenceURL] as? URL
let result = PHAsset.fetchAssets(withALAssetURLs: [assetPath], options: nil)
let asset = result.firstObject
let fileName = asset?.value(forKey: "filename")
let fileUrl = URL(string: fileName as! String)
if let name = fileUrl?.deletingPathExtension().lastPathComponent
print(name)
//let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL
if (assetPath.absoluteString.hasSuffix("JPG"))
sonu = "JPG"
print("JPG")
else if (assetPath.absoluteString.hasSuffix("PNG"))
sonu = "PNG"
print("PNG")
else if (assetPath.absoluteString.hasSuffix("GIF"))
sonu = "GIF"
print("GIF")
else
sonu = "Unknown"
print("Unknown")
return name
return ""
【讨论】:
以上是关于如何在按钮上显示图像名称?当我们上传它?的主要内容,如果未能解决你的问题,请参考以下文章