gpt4 book ai didi

ios - 从 UIImagePickerViewController 给出的路径快速加载图库中的图像?

转载 作者:行者123 更新时间:2023-11-30 13:40:06 27 4
gpt4 key购买 nike

我正在通过 UIImagepicker 从图库返回数据库中保存路径

 let imageURL = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageURL.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String

//let localPath = documentDirectory.stringByAppendingPathComponent(imageName!)

let imagePath = String("\(documentDirectory)/\(imageName!)")

路径看起来像

/Users/kshitijghule/Library/Developer/CoreSimulator/Devices/3AC7B500-E980-4A00-BC32-34FA3C7DDE8A/data/Containers/Data/Application/838FB5A1-4448-46C9-8BEC-2E939668B97D/Documents/asset.JPG

现在我想从这个路径加载这个图像。

i used this code 


let url = NSURL(string:"/Users/kshitijghule/Library/Developer/CoreSimulator/Devices/3AC7B500-E980-4A00-BC32-34FA3C7DDE8A/data/Containers/Data/Application/838FB5A1-4448-46C9-8BEC-2E939668B97D/Documents/asset.JPG")!//path from database but mention static
let imageData = NSData(contentsOfURL:url)!
cell?.petImageView.image = UIImage(data: imageData)

但是我在线路上崩溃了

let imageData = NSData(contentsOfURL:url)! 

如何解决这个问题?它可以在设备上运行吗?

最佳答案

如果您将图像存储在文档目录中,则仅将图像名称存储在数据库中

在检索时,将当前文档目录路径附加到您的图像名称,就像在保存时所做的那样

let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String

//let localPath = documentDirectory.stringByAppendingPathComponent(imageName!)
//imageName which you have stored in the db at retrieval time
let imagePath = String("\(documentDirectory)/\(imageName!)")

let url = NSURL(string:imagePath)
let imageData = NSData(contentsOfURL:url)!

因为我最后也遇到了此类问题,如果发现模拟器的应用程序编号(838FB5A1-4448-46C9-8BEC-2E939668B97D)发生变化而导致问题

希望这可以解决您的问题

关于ios - 从 UIImagePickerViewController 给出的路径快速加载图库中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35678980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com