Ios 从解析(Swift)将图像检索到UiimageView
我一直在尝试将Parse.com中的图像加载到故事板中的uiimageview中 我上传了一张图片进行解析。理想情况下,它会通过ID调用我的图像,然后继续在uiimageView中显示它。我到处寻找答案,但大多数都是针对Obj-C的,或者只包含调用图像的一半代码 我的目标是要显示的图像,我可以每天更新 这是到目前为止我的代码。它运行时没有错误,但没有显示图像Ios 从解析(Swift)将图像检索到UiimageView,ios,swift,parse-platform,uiimageview,Ios,Swift,Parse Platform,Uiimageview,我一直在尝试将Parse.com中的图像加载到故事板中的uiimageview中 我上传了一张图片进行解析。理想情况下,它会通过ID调用我的图像,然后继续在uiimageView中显示它。我到处寻找答案,但大多数都是针对Obj-C的,或者只包含调用图像的一半代码 我的目标是要显示的图像,我可以每天更新 这是到目前为止我的代码。它运行时没有错误,但没有显示图像 override func viewDidLoad() { super.viewDidLoad() //
override func viewDidLoad() {
super.viewDidLoad()
//Print out a message if we're able to get to this part of the program.
print("Reached getProductData function. Now asking for data.")
getProductData()
print("done");
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getProductData () //-> UIImage
{
let query: PFQuery = PFQuery(className: "UserPhoto")
var gridimage = UIImage()
print("pass");
//call function to get the data from parse by specifyng an objectId
query.getObjectInBackgroundWithId("XXXXXXXXXX") {
(productData:PFObject?, error:NSError?) -> Void in
if error == nil && productData != nil {
//Extract values from the productData PFObject and store them in constants
//-----start image loading
print("ok")
if let productImageFile = productData!["productImage"] as? PFFile {
productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
//here I changed var gridimage to just gridimage
gridimage = UIImage(data:imageData!)!
self.eliegrid.image = UIImage(data:imageData!)
print("next")
}
else
{
print("error")
}
}
}
//-----end image loading
} else if error != nil {
print("Could not load data from Parse")
}
}
//return gridimage
}
}
它似乎跳过了这一块:
if let productImageFile = productData!["productImage"] as? PFFile {
编辑:因为它永远不会打印下一个
我也退房了
但它仍然不起作用
我对Parse和Swift都是新手,可能缺少一些简单的东西。任何帮助都将不胜感激
更新:
谢谢@beyowulf!以下是工作代码:
func getProductData ()
{
let query: PFQuery = PFQuery(className: "UserPhoto")
var gridimage = UIImage()
print("pass");
//call function to get the data from parse by specifyng an objectId
query.getObjectInBackgroundWithId("cF0h6RVcKu") {
(productData:PFObject?, error:NSError?) -> Void in
if error == nil && productData != nil {
//Extract values from the productData PFObject and store them in constants
//-----start image loading
print("ok")
print(productData)
if let productImageFile = productData!["imageFile"] as? PFFile {
productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
print("error")
if error == nil {
//here I changed var gridimage to just gridimage
gridimage = UIImage(data:imageData!)!
self.eliegrid.image = UIImage(data:imageData!)
print("next")
}
}
}
}
else
{
print("error")
}
}
}
}移动:
func getProductData () -> UIImage
{
var gridimage = UIImage()
print("pass");
//call function to get the data from parse by specifyng an objectId
query.getObjectInBackgroundWithId("XXXXXXXXXX") {
(productData:PFObject?, error:NSError?) -> Void in
if error == nil && productData != nil {
//Extract values from the productData PFObject and store them in constants
//-----start image loading
print("ok")
//var imageFromParse = object?.objectForKey("imageNews") as? PFFile
//imageFromParse!.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
//var image: UIImage! = UIImage(data: imageData!)!
//cell?.imageViewCell?.image = image
//})
if let productImageFile = productData!["productImage"] as? PFFile {
productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
var gridimage = UIImage(data:imageData!)!
self.eliegrid.image = UIImage(data:imageData!)
print("next")
}
}
}
} else if error != nil {
print("Could not load data from Parse")
}
}
return gridimage
}
不在视图中加载然后删除:
print("done");
self.eliegrid.image = getProductData()
从viewDidLoad的底部。您正在调用一个在完成块可以执行之前超出范围的函数,这就是为什么它没有被触发。以这种方式嵌套函数不是一件非常迅速的事情,而且几乎从来都不可取
还可以尝试更改:
if let productImageFile = productData!["productImage"] as? PFFile {
productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
var gridimage = UIImage(data:imageData!)!
self.eliegrid.image = UIImage(data:imageData!)
print("next")
}
}
}
致:
你确定你在解析图像吗?你能在parse.com上查一下吗?是的,我在parse.com上有一张图片。我已经检查并再次检查了我的后端在线。感谢您的快速回复。我将把所有代码移到哪里?在viewDidLoad结尾的}下面。它只需要是它自己的函数,而不是嵌套在另一个函数中。我尝试了你们提供的,但它仍然没有出现在uiimageview中。检查我的原始问题,查看编辑的代码!它打印什么?它打印通行证吗?好啊下一步?打印:到达getProductData函数。现在要求提供数据。通过,好的,有什么想法吗?
let productImageFile = productData.valueForKey("productImage") as! PFFile
productImageFile.getDataInBackgroundWithBlock {(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
var gridimage = UIImage(data:imageData!)!
self.eliegrid.image = UIImage(data:imageData!)
print("next")
}
}