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

how I show image name on button ? When we Uploading it?

我正在通过点击按钮上传图片,我想在同一个按钮(我用来上传图片的按钮)上显示图片名称,我该怎么做?

    @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 select 图片并上传到服务器时。请按照以下步骤获取文件名

步骤 1.
import AssetsLibrary import Photos

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

第 3 步。Image Picker Controller 委托方法获取 selected 图像的信息

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)

}

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

这是在按钮上获取任何图像名称的完整正确代码。当我们上传它时。

    @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 ""

}