How can I get the name of the picture after the selection?

как бы я не пробовал он выдает: Optional("<UIImage: 0x7fa61944c3d0>") 

Code:

 @IBAction func tappedBgButton(sender: AnyObject) { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) { print("chooseImage Button: ") imagePicked = 2 print(imagePicked) self.presentViewController(imagePicker, animated: true, completion: nil) } } @IBAction func tappedAvatarButton(sender: AnyObject) { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) { print("chooseImage Button: ") imagePicked = 1 print(imagePicked) self.presentViewController(imagePicker, animated: true, completion: nil) } } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage if imagePicked == 1 { userAva.image = pickedImage } else if imagePicked == 2 { userBG.image = pickedImage } dismissViewControllerAnimated(true, completion: nil) } 

    1 answer 1

    In your 3rd function add:

     func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { if let pickedImageURL = info[UIImagePickerControllerOriginalImage] as? NSURL { ALAssetsLibrary().assetForURL(pickedImageURL , resultBlock: { asset in let imageName = asset.defaultRepresentation().filename() //imageName получено, используйте в своих целях }, failureBlock: nil) } } ... } 

    or

     func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { if let pickedImageURL = info[UIImagePickerControllerOriginalImage] as? NSURL { let result = PHAsset.fetchAssetsWithALAssetURLs([pickedImageURL], options: nil) let imageName = result.firstObject?.filename ?? "" } ... } 
    • not that not other option does not work writes Cannot convert value of type 'UIImage?' to expected element type 'NSURL' - Malyshev
    • @Malyshev did not inspect, corrected, updated the answer - VAndrJ
    • thanks, all worked well! - Malyshev