我正在通过UIImagepicker从Gallery中保存数据库返回路径
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)!
如何解决这个问题?它可以在设备上工作吗?
3 回答
如果要在文档目录中存储图像,则仅在db中存储图像名称
在检索时,使用您在保存时完成的图像名称附加当前文档目录路径
因为如果发现模拟器的应用程序编号(838FB5A1-4448-46C9-8BEC-2E939668B97D)是导致问题的更改,我最后也会遇到此类问题
希望这可以解决您的问题
我已经回答了类似的问题here . 您只需将这些路径保存到数据库,并从数据库路径中检索这些图像 .
将图像保存在从图库或相机中获取的文档目录中 .
从数据库加载图像