简体   繁体   English

使用Swift从Parse加载图像

[英]Loading images from Parse using Swift

I've written my code for retrieving images from my Parse data console so that they will appear in my Storyboard for my Swift app, but I'm not sure how to make an IBOutlet connect to the UIImageView I added to my Storyboard so that the image from Parse will actually appear in its place. 我编写了用于从Parse数据控制台检索图像的代码,以便它们出现在我的Swift应用程序的Storyboard中,但我不知道如何让IBOutlet连接到我添加到我的Storyboard的UIImageView,以便来自Parse的图像实际上会出现在它的位置。 Here's the code I have so far in my ViewController: 这是我在ViewController中到目前为止的代码:

var imageResources : Array<UIImage> = []

override func viewDidLoad() {
    super.viewDidLoad()


func loadImages(){

    var query2 = PFQuery(className: "VoteCount")
    query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
        if(error == nil){
            let imageObjects = objects as [PFObject]
            for object in objects {
                let thumbNail = voteCount1["image"] as PFFile
                thumbNail.getDataInBackgroundWithBlock({
                    (imageData: NSData!, error: NSError!) -> Void in
                    if (error == nil) {
                        let image = UIImage(data:imageData)
                        self.imageResources.append(image!)
                        println(image)
                    }
                })
            }
        }
        else{
            println("Error in retrieving \(error)")
        }
        })
    }

}

What kind of IBOutlet do I make that connects to the UIImageView I've added to my Main.storyboard? 我将哪种IBOutlet连接到我添加到Main.storyboard的UIImageView? I'm just not sure what to name it that will call the image from Parse that I'm trying to retrieve. 我只是不确定如何命名它将从Parse调用我想要检索的图像。

control-drag from your UIImageView to your Swift file, outside the methods but inside the implementation. 控制 - 从UIImageView拖动到Swift文件,在方法之外但在实现中。 name it myImageView. 将其命名为myImageView。

You can't call println on an image. 您无法在图像上调用println。

Then after the line if (error == nil) { try the following 然后在行if (error == nil) {尝试以下

if let image = UIImage(data:imageData) {
    dispatch_async(dispatch_get_main_queue()) {
         self.myImageView.image = image
    }
}

Or something to that effect. 或者那种效果。 You need to be sure: 你需要确定:

  1. the image exists 图像存在
  2. you do anything with UI updates on the main thread (that's what dispatch_async(dispatch_get_main_queue()) {} accomplishes 你在主线程上用UI更新做什么(这就是dispatch_async(dispatch_get_main_queue()) {}完成的事情

Try it and let me know if that helps. 尝试一下,让我知道这是否有帮助。 I'm on the road right now but when I get a chance to sit I'll circle back and check this out. 我现在正在路上,但是当我有机会坐下来时,我会回过头来检查一下。

One last trick: you can put a breakpoint on lines where you get an image. 最后一招:您可以在获得图像的行上放置断点。 Then when the application pauses, you can hover over the word image in the line if let image = UIImage(data:imageData) { and click on the eye icon in the popup. 然后,当应用程序暂停时, if let image = UIImage(data:imageData) {并单击弹出窗口中的眼睛图标,则可以将鼠标悬停在该行中的单词image上。 You should then be able to view the image from Parse. 然后,您应该能够从Parse查看图像。 If the image doesn't appear when you click the eye icon, you probably have additional debugging to do (does the image exist, is your logic up to that point correct, etc). 如果单击眼睛图标时没有出现图像,则可能需要进行其他调试(图像是否存在,您的逻辑是否正确,等等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM