{"id":1337,"date":"2020-12-17T14:24:04","date_gmt":"2020-12-17T06:24:04","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1337"},"modified":"2020-12-17T14:24:04","modified_gmt":"2020-12-17T06:24:04","slug":"swiftui-%e8%af%bb%e5%8f%96%e7%bd%91%e7%bb%9c%e5%9b%be%e7%89%87-%e5%b9%b6%e4%b8%94%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1337","title":{"rendered":"SwiftUI \u8bfb\u53d6\u7f51\u7edc\u56fe\u7247 \u5e76\u4e14\u7f13\u5b58"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import Foundation\nimport SwiftUI\n\nstruct ImageWithURL: View {\n    \n    @ObservedObject var imageLoader: ImageLoaderAndCache\n\n    init(_ url: String) {\n        imageLoader = ImageLoaderAndCache(imageURL: url)\n    }\n\n    var body: some View {\n          Image(uiImage: UIImage(data: self.imageLoader.imageData) ?? UIImage())\n              .resizable()\n              .clipped()\n    }\n}\n\nclass ImageLoaderAndCache: ObservableObject {\n    \n    @Published var imageData = Data()\n    \n    init(imageURL: String) {\n        let cache = URLCache.shared\n        let request = URLRequest(url: URL(string: imageURL)!, cachePolicy: URLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 60.0)\n        if let data = cache.cachedResponse(for: request)?.data {\n            print(\"got image from cache\")\n            self.imageData = data\n        } else {\n            URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in\n                if let data = data, let response = response {\n                let cachedData = CachedURLResponse(response: response, data: data)\n                                    cache.storeCachedResponse(cachedData, for: request)\n                    DispatchQueue.main.async {\n                        print(\"downloaded from internet\")\n                        self.imageData = data\n                    }\n                }\n            }).resume()\n        }\n    }\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct ContentView: View {\n\n    var body: some View {\n        ImageWithURL(\"https:\/\/i.redd.it\/mn5m2km7hmv01.jpg\")\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1337"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1337"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1337\/revisions"}],"predecessor-version":[{"id":1338,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1337\/revisions\/1338"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}