How to get file name in UIImagePickerController with Asset library?

后端 未结 13 1616
天命终不由人
天命终不由人 2020-12-03 14:55

I want to get the file name from UIImagePickerController. I do not want to use ALAssetLibrary because it is deprecated in iOS 9. I have used the following code

相关标签:
13条回答
  • 2020-12-03 15:58

    Code:

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
        let imageName = imageURL.path!.lastPathComponent
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
        let localPath = documentDirectory.stringByAppendingPathComponent(imageName)
        let image = info[UIImagePickerControllerOriginalImage] as UIImage
        let data = UIImagePNGRepresentation(image)
        data.writeToFile(localPath, atomically: true)
        let imageData = NSData(contentsOfFile: localPath)!
        let photoURL = NSURL(fileURLWithPath: localPath)
        let imageWithData = UIImage(data: imageData)!
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
    
    0 讨论(0)
提交回复
热议问题