我如何在按钮上显示图像名称?当我们上传它?

时间:2018-03-06 05:35:10

标签: swift uibutton uiimageview uiimagepickercontroller

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

<select name="selectedValue">
    <option value="Newest">Newest</option>
    <option value="Best Sellers">Best Sellers</option>
    <option value="Alphabetical">Alphabetical</option>
</select>

2 个答案:

答案 0 :(得分:0)

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

步骤1.
import AssetsLibrary import Photos

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

步骤3.图像选择器控制器委托方法以获取所选图像的信息

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搜索来解决一些警告。 如果这个答案可以帮助你,请投票支持。

答案 1 :(得分:0)

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

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

}