我如何进入firebase获取我的图像url然后解码成UIImage然后在谷歌地图中作为标记图标显示图片



我的代码:下面是我的代码!

       func queryImageWithCompletion(completion:((image:UIImage?)->Void)?) {
            self.ref.child("users").child((userID)!).observeEventType(.Value, withBlock: { snapshot in
                if let strUrl = snapshot.value as? String, url = NSURL(string: strUrl) {
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
                        if let data = NSData(contentsOfURL: url), image = UIImage(data:data) {
                            dispatch_async(dispatch_get_main_queue(), {
                                completion?(image:image)
                                marker.icon = UIImage(data: data)
                            })
                        } else {
                            dispatch_async(dispatch_get_main_queue(), {
                                completion?(image:nil)
                            })
                        }
                    })
                } else {
                    dispatch_async(dispatch_get_main_queue(), {
                        completion?(image:nil)
                    })
                }
            })
        }

,这是我正在使用的代码,试图显示图片作为一个标记图标!有人能帮我吗?我没有得到任何错误,但它不会工作

由于网络操作是异步的,我建议您使用以下命令:

func queryImageWithCompletion(completion:((image:UIImage?)->Void)?) {
    ref.child("users").child(String(authorId)).child("profileUrl").observeEventType(.Value, withBlock: { snapshot in
        if let strUrl = snapshot.value as? String, url = NSURL(string: strUrl) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { 
                if let data = NSData(contentsOfURL: url), image = UIImage(data:data) {
                    dispatch_async(dispatch_get_main_queue(), { 
                        completion?(image:image)
                    })
                } else {
                    dispatch_async(dispatch_get_main_queue(), {
                        completion?(image:nil)
                    })
                }
            })
        } else {
            dispatch_async(dispatch_get_main_queue(), {
                completion?(image:nil)
            })
        }
    })
}

相关内容

最新更新