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)
}
}
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 ""
}