UIImage Gif 加载委托?

UIImage Gif loading delegate?

我正在使用this guy's Gif class to create a Gif UIImage. https://github.com/mayoff/uiimage-from-animated-gif

用这个加载它:

let fileUrl = NSURL(string: "some url" as! String)
let Gif = UIImage.animatedImageWithAnimatedGIFURL(fileUrl!)
imageview.image=Gif

问题是它需要时间,很多时间,比如 7 秒才能看到图像。

我想以某种方式预加载它,或者至少在完成加载时得到一个委托。

我有哪些选择?

您可以使用 Grand Central Dispatch(或 GCD)在后台执行此任务。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    // This will happen in the background
    let fileUrl = NSURL(string: "some url" as! String)
    let gif = UIImage.animatedImageWithAnimatedGIFURL(fileUrl!)

    dispatch_async(dispatch_get_main_queue()) {
        // Always update UI on the main thread
        imageview.image = gif
    }
}

无论加载 gif 需要多长时间,这仍然需要花费很长时间,但它不会锁定 UI,因此用户可以在加载 gif 时执行操作。