我一直在尝试将Parse.com中的图像加载到我的故事板内部的uiimageview中。
我将图片上传到Parse。理想情况下,它会通过ID调用我的图像,然后继续在uiimageView中显示它。我已经搜索了高和低的答案,但大多数都是Obj-C或只包含一半的代码来调用图像。
我的目标是显示一张图像,我可以每天更新。
到目前为止,这是我的代码。它运行没有错误但没有图像出现。
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 {
编辑:因为它从不打印“下一个”。
我也检查了
iOS - Retrieve and display an image from Parse in UIImageView (Swift 1.2 Error)
但它仍然不起作用。
我对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")
}
}
}
}
答案 0 :(得分:-1)
移动:
let firstChannel = buffer.floatChannelData[0]
let arr = Array(UnsafeBufferPointer(start: firstChannel, count: Int(buffer.frameLength)))
// Do something with your array of Floats
out of viewDidLoad然后删除:
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
}
从viewDidLoad的底部。您正在调用超出范围的函数,然后才能执行完成块,这就是它不会触发的原因。以这种方式嵌套函数并不是一件非常迅速的事情,几乎不可能。
也可以尝试更改:
print("done");
self.eliegrid.image = getProductData()
要:
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")
}
}
}