0

I am uploading an image via click on button and I want to show image name on same button (Which I am using to upload image) how can I do ?

    @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)
        }
     }
  • check this link to make use of `UIImagePickerController` : https://stackoverflow.com/questions/24625687/swift-uiimagepickercontroller-how-to-use-it – technerd Mar 06 '18 at 05:41
  • Follow this link to get image name from library: https://stackoverflow.com/questions/40627901/how-to-get-file-name-in-uiimagepickercontroller-with-asset-library – Sagar Chauhan Mar 06 '18 at 05:45

2 Answers2

0

As you are using the UIImagePickerController to select the image and upload on the server. Please follow the below steps to get the file name

Step 1.
import AssetsLibrary import Photos

Step 2. Make sure in Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

Step 3. Image Picker Controller delegate method to get the info of the selected image

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)

}

NOTE: there can be some warning which you can resolve by looking into the documentation or google search. If this answer helps you please vote for it.

Karamjeet Singh
  • 460
  • 1
  • 5
  • 16
0

It is the complete and Proper code to get any image name on button. When we upload it.

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

}