使用有效的 UIImage 设置 UIImageView.image 会在 swift 中创建 "nil" 错误
Setting UIImageView.image with valid UIImage creates "nil" error in swift
我正在尝试在 UIImageView 上设置图像 属性。当我使用 UIImage 设置 .image 属性 时,它每次都会抛出此错误:
"unexpectedly found nil while unwrapping an Optional value"
问题是我的 UIImage 不是 nil。
这是我设置 UIImage 的代码
func setPhotosForNewsItem(photoArray:[Photo]) {
println("Image Count: " + String(photoArray.count))
var image:UIImage = photoArray[0].photo
println(image.description)
self.newsImage.image = image
}
控制台输出如下:
Image Count: 2
UIImage: 0x7fdd93c5cdd0, {1115, 1115}
fatal error:
unexpectedly found nil while unwrapping an Optional value (lldb)
我可以使用 xCode 中的“快速查看”工具在我假定为 nil 的 UIImage 上查看我正在尝试使用的照片。当我的 UIImage 显然不是 nil 时,为什么我会抛出 nil 错误?
更新::
我似乎没有正确地将 UIImage 存储在我的数组中。这里是我下载图像并将它们存储到我的阵列以供稍后解压的地方。
var relatedPhotos:[PFObject] = relations as! [PFObject]
//println(relations!)
var photoArray:[Photo] = [Photo]()
for photo in relatedPhotos {
var newPhoto = Photo()
var photoFile:PFFile = photo.objectForKey("photo") as! PFFile
newPhoto.object = photo
newPhoto.objectID = photo.objectId!
photoFile.getDataInBackgroundWithBlock({(imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
newPhoto.photo = UIImage(data:imageData!)!
//println(newPhoto.photo)
photoArray.append(newPhoto)
if photoArray.count == relatedPhotos.count {
if newObject is FieldReport {
var report = newObject as! FieldReport
report.photos = photoArray
updatedReports.append(report)
//println("Report Count 1: " + String(updatedReports.count))
}
else {
var report = newObject as! Feature
report.photos = photoArray
updatedReports.append(report)
}
if updatedReports.count == objects.count {
self.delegate?.fieldReports(updatedReports)
}
}
}
})
}
我知道这可以下载和显示照片,因为我刚刚成功地使用过它。对我来说,这意味着我没有正确存储 UIImage。我应该以不同的方式存储这些图像文件吗?
您可以通过安全展开来防止崩溃的发生
func setPhotosForNewsItem(photoArray:[Photo]) {
println("Image Count: " + String(photoArray.count))
if var image:UIImage = photoArray[0].photo
{
println(image.description)
self.newsImage.image = image
}
}
设置断点并检查错误
编辑:
我唯一能做的事 - 检查这里发生了什么:
newPhoto.photo = UIImage(data:imageData!)!
似乎这是所有问题的主要原因。检查图像数据的类型,尝试通过例如 UIImage(CGImage: <#CGImage!#>)
将其转换为图像。你需要弄清楚如何处理这个图像。
我正在尝试在 UIImageView 上设置图像 属性。当我使用 UIImage 设置 .image 属性 时,它每次都会抛出此错误:
"unexpectedly found nil while unwrapping an Optional value"
问题是我的 UIImage 不是 nil。
这是我设置 UIImage 的代码
func setPhotosForNewsItem(photoArray:[Photo]) {
println("Image Count: " + String(photoArray.count))
var image:UIImage = photoArray[0].photo
println(image.description)
self.newsImage.image = image
}
控制台输出如下:
Image Count: 2 UIImage: 0x7fdd93c5cdd0, {1115, 1115} fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
我可以使用 xCode 中的“快速查看”工具在我假定为 nil 的 UIImage 上查看我正在尝试使用的照片。当我的 UIImage 显然不是 nil 时,为什么我会抛出 nil 错误?
更新::
我似乎没有正确地将 UIImage 存储在我的数组中。这里是我下载图像并将它们存储到我的阵列以供稍后解压的地方。
var relatedPhotos:[PFObject] = relations as! [PFObject]
//println(relations!)
var photoArray:[Photo] = [Photo]()
for photo in relatedPhotos {
var newPhoto = Photo()
var photoFile:PFFile = photo.objectForKey("photo") as! PFFile
newPhoto.object = photo
newPhoto.objectID = photo.objectId!
photoFile.getDataInBackgroundWithBlock({(imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
newPhoto.photo = UIImage(data:imageData!)!
//println(newPhoto.photo)
photoArray.append(newPhoto)
if photoArray.count == relatedPhotos.count {
if newObject is FieldReport {
var report = newObject as! FieldReport
report.photos = photoArray
updatedReports.append(report)
//println("Report Count 1: " + String(updatedReports.count))
}
else {
var report = newObject as! Feature
report.photos = photoArray
updatedReports.append(report)
}
if updatedReports.count == objects.count {
self.delegate?.fieldReports(updatedReports)
}
}
}
})
}
我知道这可以下载和显示照片,因为我刚刚成功地使用过它。对我来说,这意味着我没有正确存储 UIImage。我应该以不同的方式存储这些图像文件吗?
您可以通过安全展开来防止崩溃的发生
func setPhotosForNewsItem(photoArray:[Photo]) {
println("Image Count: " + String(photoArray.count))
if var image:UIImage = photoArray[0].photo
{
println(image.description)
self.newsImage.image = image
}
}
设置断点并检查错误
编辑:
我唯一能做的事 - 检查这里发生了什么:
newPhoto.photo = UIImage(data:imageData!)!
似乎这是所有问题的主要原因。检查图像数据的类型,尝试通过例如 UIImage(CGImage: <#CGImage!#>)
将其转换为图像。你需要弄清楚如何处理这个图像。