作者:佳君旭博21 | 来源:互联网 | 2023-05-24 09:12
我正在尝试从照片库中提供用户选择的图像.因此我正在UIImagePickerController
选择.但是这里出现了在文件系统中获取其初始URL的问题(我需要这个CKAsset
).
我的代码.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
let path = imageURL.path!
let imageName = path.lastPathComponent
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory = paths.first as String!
let localPath = documentDirectory + "/" + imageName
let imageData = NSData(contentsOfFile: localPath)!
let image = UIImage(data: imageData)!
picker.dismissViewControllerAnimated(true, completion: nil)
}
imageURL有点神秘.它描述了资产URL,但不是文件系统中的资产URL.它看起来像:assets-library://asset/asset.JPG?id=B6C0A21C-07C3-493D-8B44-3BA4C9981C25&ext=JPG
.
根据该逻辑路径是/asset.JPG
其中asset.JPG
是所述图像的名称.
然后我正在访问我的Documents文件夹并试图找到一个带路径的文件:
/Users/Eugene/Library/Developer/CoreSimulator/Devices/3C5E9A23-8220-4B37-BD14-F1E42EEC2C7C/data/Containers/Data/Application/20EBAAE2-6C6F-4651-A48F-16A222CCB3A2/Documents/asset.JPG
也是隐秘的,但似乎是不存在的图像的真实路径......没有找到具有该路径的图像.这让我感到恶心.
我是否必须从头开始保存图像?或者还有其他方法吗?
我已经使用AssetsLibrary API 查看了几个教程,但我找不到任何有用的方法来解决我的问题.先感谢您!
1> mozharovsky..:
好的,我已经解决了这个问题.
您只需抓取图像(info[UIImagePickerControllerOriginalImage] as UIImage
)并保存到给定目录即可.如果你只需要保存1张图片就可以了.代码ID如下.
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)
}
我没有在Swift 3中工作,我转换代码使其编译但返回的路径并未指向所选图像
你能否为Swift 2更新你的答案?谢谢!
2> Jaydip..:
在SWIFT 4你可以尝试这个,它正常工作.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{
let imgName = imgUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let localPath = documentDirectory?.appending(imgName)
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let data = UIImagePNGRepresentation(image)! as NSData
data.write(toFile: localPath!, atomically: true)
//let imageData = NSData(contentsOfFile: localPath!)!
let photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!)
print(photoURL)
}
APPDEL.window?.rootViewController?.dismiss(animated: true, completion: nil)
}