[英]Can't load image with UIImage or NSData
I am programming a Swift application, and I can't load image saved in the application by using UIImage(contentsOfFile: imgPath)
or NSData(contentsOfFile: imgPath)
我正在编写一个Swift应用程序,但无法使用
UIImage(contentsOfFile: imgPath)
或NSData(contentsOfFile: imgPath)
加载保存在应用程序中的图像
private func loadData() {
println("load DATA DANGEROUS")
let (dangerous, err) = SD.executeQuery("SELECT * FROM Dangerous")
if err == nil && !dangerous.isEmpty {
var tabPhoto : [DangerousImage] = []
for d in dangerous {
let desc = d["description"]!.asString()!
let idDangerous = d["id"]!.asInt()!
println("iddangerous : \(idDangerous)")
let (photos, error) = SD.executeQuery("SELECT * FROM Photo WHERE idDangerous = ?", withArgs: [idDangerous])
if error == nil {
for photo in photos {
let imgPath = photo["photoPath"]!.asString()!
println(imgPath)
let uimage = UIImage(contentsOfFile: imgPath) // fatal error: unexpectedly found nil while unwrapping an Optional value
tabPhoto.append(DangerousImage(img: uimage!, path: imgPath))
}
}
println("add ENTRY")
self.tabEntry.append(Entry(descript: desc, tab: tabPhoto))
}
}
println("TAB ENTRY : \(tabEntry)")
}
My picture exists with this path : /var/mobile/Containers/Data/Application/...ID-APP.../Documents/images/JPEG_201506162_101128_IOS_99804574.jpg
我的图片存在以下路径:/
/var/mobile/Containers/Data/Application/...ID-APP.../Documents/images/JPEG_201506162_101128_IOS_99804574.jpg
Thank for your help. 谢谢您帮忙。
Ysee Ysee
From I can see in your code, you are storing the full path to the image in your database, eg. 从我的代码中可以看出,您正在将图像的完整路径存储在数据库中。 "/var/mobile/Containers/Data/Application/...ID-APP.../Documents/images/JPEG_201506162_101128_IOS_99804574.jpg" .
“ / var / mobile /容器/数据/应用程序/...ID-APP.../文档/图像/JPEG_201506162_101128_IOS_99804574.jpg” 。 Since iOS8, the folder structure has changed - the UDID in the path is changing every time the app is updated or a new build is installed during development.
自iOS8起,文件夹结构已更改-每次在开发过程中更新应用程序或安装新版本时,路径中的UDID都会更改。 That's why should store a relative path to your image, eg.
这就是为什么应该存储图像的相对路径的原因。 "/images/JPEG_201506162_101128_IOS_99804574.jpg" and then get the Documents directory with
NSSearchPathForDirectoriesInDomains
method. “ /images/JPEG_201506162_101128_IOS_99804574.jpg” ,然后使用
NSSearchPathForDirectoriesInDomains
方法获取Documents目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.