Home > AI > Uncategorized

ios – 同步异步加载网络图片和lottie动画库

参考:http://blog.csdn.net/u014636932/article/details/74913498

Mac装了几个虚拟主机,手机上网时不能访问,只能用192.168.1.101局域网地址,用127.0.0.1和www.boygirl88.com等虚拟主机地址不行。

还有用wifi连接可以,4g不行,手机检查已经打开了4g,不知为什么。

网络第三,info.plist打开App Transport Security,这个都知道。^^

同步加载图片,没多大用。

//wifi is good, 4g is no
        //同步:加载图片时界面空白
        //速度慢,不稳定
       // let url3 = URL(string:"http://i.imgur.com/w5rkSIj.jpg")
        let url3 = URL(string:"http://192.168.1.3/texture.png")
        /*
        let data = try? Data(contentsOf: url3!)
        if let imageData = data {
            let image = UIImage(data: imageData)
            let imageView = UIImageView(image:image)
            imageView.frame = self.view.frame
            self.view.addSubview(imageView)
        }
         */

异步加载图片

//异步
        /*
        let session = URLSession.shared.dataTask(with: url3!) { (data, response, error) in
            if let e = error {
                print(e.localizedDescription)
            }
            if let re = response as? HTTPURLResponse {
                if let imgData = data{
                    DispatchQueue.main.async {
                        let image = UIImage(data:imgData)
                        let imageView = UIImageView(image: image)
                        imageView.frame = self.view.frame
                        self.view.addSubview(imageView)
                    }
                } else{
                    print ("no data")
                }
            } else {
                print("no response")
            }
        }
        session.resume()
        */

最后,加载lottie动画,直接上码:

//local
        //let animView = LOTAnimationView(name: "50-CryingEmoji.json")
        //let animView = LOTAnimationView(contentsOf: URL(string: "http://www.boygirl88.com/test/50-CryingEmojicc.json")!)//这样地址,不行
        //local server
        let animView = LOTAnimationView(contentsOf: URL(string: "http://192.168.1.3/lottie/50-CryingEmoji.json")!)
        animView.center = self.view.center
        animView.frame = CGRect(x: 0, y: 0, width: 400, height: 400)
        animView.contentMode = .scaleAspectFill
        self.view.addSubview(animView)
        animView.play()

 

 

 

 

 

Related posts:

Leave a Reply