{"id":1309,"date":"2020-12-11T15:54:30","date_gmt":"2020-12-11T07:54:30","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1309"},"modified":"2020-12-11T15:54:30","modified_gmt":"2020-12-11T07:54:30","slug":"ios14-widget%e5%b0%8f%e7%bb%84%e4%bb%b6%e5%bc%80%e5%8f%91%e5%ae%9e%e8%b7%b55-%e7%bd%91%e7%bb%9c%e5%9b%be%e7%89%87%e7%9a%84%e5%8a%a0%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1309","title":{"rendered":"iOS14 Widget\u5c0f\u7ec4\u4ef6\u5f00\u53d1\u5b9e\u8df55\u2014\u2014\u7f51\u7edc\u56fe\u7247\u7684\u52a0\u8f7d"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/12\/image-24-1024x568.png\" alt=\"\" class=\"wp-image-1310\" width=\"490\" height=\"271\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-24-1024x568.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-24-300x166.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-24-768x426.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-24-830x460.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-24-230x128.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-24-350x194.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-24-480x266.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-24.png 1320w\" sizes=\"(max-width: 490px) 100vw, 490px\" \/><\/figure><\/div>\n\n\n\n<p>\u5728<code>SwiftUI<\/code>\u4e2d\uff0c\u901a\u8fc7<code>Image<\/code>\u6765\u5c55\u793a\u56fe\u7247\uff0c\u4f8b\u5982\uff1a<\/p>\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=\"\">Image(\"touxiang\")\n\nImage(systemName: \"arkit\")\n    .foregroundColor(.orange)\n    .font(.system(size: 48))\n\nImage(\"touxiang\")\n    .resizable()\n    .aspectRatio(contentMode: .fit)\n\nImage(uiImage: UIImage(named: \"touxiang\"))\n        .resizable()\n        .frame(width: 50, height: 50)<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u90fd\u662f\u4f7f\u7528<code>Image<\/code>\u52a0\u8f7d\u672c\u5730\u56fe\u7247\u8d44\u6e90\uff0c\u4f46\u662f<code>SwiftUI<\/code>\u4e2d\u7684<code>Image<\/code>\u6ca1\u6709\u63d0\u4f9b\u76f4\u63a5\u52a0\u8f7d<code>URL<\/code>\u65b9\u5f0f\u7684\u56fe\u7247\u663e\u793a\uff0c\u90a3\u4e48\u5982\u4f55\u5728<code>SwiftUI<\/code>\u4e2d\u8ba9<code>Image<\/code>\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\u5462\uff0c\u53ef\u4ee5\u91c7\u7528\u5f02\u6b65\u52a0\u8f7d\u7f51\u7edc\u56fe\u7247\u6570\u636e\uff0c\u7531<code>data<\/code>\u8f6c\u6362\u6210<code>UIimage<\/code>\uff0c\u518d\u7ed9<code>Image<\/code>\u5c55\u793a<\/p>\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    @State private var remoteImage : UIImage? = nil\n    let placeholderOne = UIImage(named: \"Picture\")\n    \n    var body: some View {\n        Image(uiImage: self.remoteImage ?? placeholderOne!)\n            .onAppear(perform: fetchRemoteImage)\n    }\n    \n    func fetchRemoteImage()\n    {\n        guard let url = URL(string: \"http:\/\/hdjc8.com\/images\/logo.png\") else { return }\n        URLSession.shared.dataTask(with: url){ (data, response, error) in\n            if let image = UIImage(data: data!){\n                self.remoteImage = image\n            }\n            else{\n                print(error ?? \"\")\n            }\n        }.resume()\n    }\n}<\/pre>\n\n\n\n<p>\u4f46\u662f\u8fd9\u79cd\u5f02\u6b65\u52a0\u8f7d\u56fe\u7247\u7684\u65b9\u5f0f\u5728<code>Widget<\/code>\u4e2d\u5374\u5931\u6548\u4e86\uff0c<code>Image<\/code>\u663e\u793a\u4e0d\u4e86\u56fe\u7247\u3002<br>\u5728<code>TimelineProvider<\/code>\u7684<code>getTimeline<\/code>\u4e2d<code>completion(timeline)<\/code>\u6267\u884c\u5b8c\u4e4b\u540e\uff0c\u4e0d\u518d\u652f\u6301\u56fe\u7247\u7684\u5f02\u6b65\u56de\u8c03\u4e86\uff0c\u6240\u4ee5\u5fc5\u987b\u5728\u6570\u636e\u8bf7\u6c42\u56de\u6765\u7684\u5904\u7406\u4e2d\u91c7\u7528\u540c\u6b65\u65b9\u5f0f\uff0c\u5c06\u56fe\u7247\u7684<code>data<\/code>\u83b7\u53d6\uff0c\u8f6c\u6362\u6210<code>UIimage<\/code>\uff0c\u5728\u8d4b\u503c\u7ed9<code>Image<\/code>\u5c55\u793a\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u91cc\u7ed9<a rel=\"noreferrer noopener\" href=\"https:\/\/www.jianshu.com\/p\/f293654f0184\" target=\"_blank\">iOS14 Widget\u5c0f\u7ec4\u4ef6\u5f00\u53d1\u5b9e\u8df52\u2014\u2014\u81ea\u5b9a\u4e49Widget<\/a>\u91cc\u642d\u5efa\u7684\u53e4\u8bd7\u89c6\u56fe\u589e\u52a0\u4e00\u4e2a\u7f51\u7edc\u5c01\u9762\u56fe\u7247\u663e\u793a\uff0c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/12\/image-26-1024x509.png\" alt=\"\" class=\"wp-image-1312\" width=\"469\" height=\"233\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-26-1024x509.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-26-300x149.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-26-768x382.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-26-830x413.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-26-230x114.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-26-350x174.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-26-480x239.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/12\/image-26.png 1130w\" sizes=\"(max-width: 469px) 100vw, 469px\" \/><\/figure><\/div>\n\n\n\n<p>\u6839\u636e\u4e0a\u9762\u8bf4\u7684\uff0c\u65e2\u7136\u5f02\u6b65\u52a0\u8f7d\u56fe\u7247\u4e0d\u884c\u4e86\uff0c\u5fc5\u987b\u5728\u6570\u636e\u8bf7\u6c42\u56de\u6765\u7684\u5904\u7406\u4e2d\u91c7\u7528\u540c\u6b65\u65b9\u5f0f\uff0c\u5c06\u56fe\u7247\u7684<code>data<\/code>\u83b7\u53d6\uff0c\u8f6c\u6362\u6210<code>UIimage<\/code>\u3002\u90a3\u4e48\u6211\u4eec\u5148\u6765\u5728<code>Poetry<\/code>\u91cc\u9762\u589e\u52a0\u4e00\u4e2a<code>UIimage<\/code>\u53c2\u6570\uff1a<\/p>\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 Poetry {\n    let content: String \/\/ \u5185\u5bb9\n    let origin: String \/\/ \u540d\u5b57\n    let author: String \/\/ \u4f5c\u8005\n    var icon: UIImage? = UIImage(named: \"touxiang\") \/\/ \u56fe\u7247\n}<\/pre>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u4e2a\u514d\u8d39\u7684<code>API<\/code>\u63a5\u53e3\u6ca1\u6709\u8fd4\u56de\u56fe\u7247\u5c01\u9762\u6570\u636e\uff0c\u6240\u4ee5\u5c31\u81ea\u5df1\u7f51\u4e0a\u627e\u4e2a\u56fe\u7247\u7528\u6765\u6d4b\u8bd5\u3002\u5173\u4e8e\u56fe\u7247\u8bf7\u6c42\u7684\u65f6\u673a\uff0c\u8fd9\u91cc\u6211\u662f\u5c06\u5b83\u653e\u5728\u4e86<code>API<\/code>\u63a5\u53e3\u56de\u8c03\u540e\u5904\u7406<code>json<\/code>\u8f6c<code>model<\/code>\u7684\u8fd9\u4e00\u6b65\uff1a<\/p>\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=\"\">static func poetryFromJson(fromData data: Data) -> Poetry { \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=\"\">static func modelFromJson(fromData data: Data) -> Poetry {\n    let json = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]\n    \/\/\u56e0\u4e3a\u514d\u8d39\u63a5\u53e3\u8bf7\u6c42\u9891\u7387\u95ee\u9898\uff0c\u5982\u679c\u592a\u9891\u7e41\u4e86\uff0c\u8bf7\u6c42\u53ef\u80fd\u5931\u8d25\uff0c\u8fd9\u91cc\u505a\u4e0b\u5904\u7406\uff0c\u653e\u7f6ecrash\n    guard let data = json[\"data\"] as? [String: Any] else {\n        return Poetry(content: \"\u8bd7\u8bcd\u52a0\u8f7d\u5931\u8d25\uff0c\u8bf7\u7a0d\u5fae\u518d\u8bd5\uff01\", origin: \"\u8010\u4f9d\u798f\", author: \"\u4f5a\u540d\")\n    }\n    let content = data[\"content\"] as! String\n    let origin = data[\"origin\"] as! String\n    let author = data[\"author\"] as! String\n    \/\/\u8fd9\u91cc\u52a0\u5165\u5bf9\u56fe\u7247\u8fdb\u884c\u540c\u6b65\u8bf7\u6c42\n    var image: UIImage? = nil\n    if let iamgeData = try? Data(contentsOf: URL(string: \"https:\/\/ss1.bdstatic.com\/70cFuXSh_Q1YnxGkpoWK1HF6hhy\/it\/u=1058052610,2039041423&amp;fm=26&amp;gp=0.jpg\")!) {\n        image = UIImage(data: iamgeData)\n    }\n    return Poetry(content: content, origin: origin, author: author, icon: image)\n}<\/pre>\n\n\n\n<p>\u6700\u540e\u5728\u7ed9<code>PoetryWidgetView<\/code>\u5e03\u5c40\u754c\u9762\uff1a<\/p>\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 PoetryWidgetView : View {\n    let entry: PoetryEntry\n\n    var body: some View {\n        HStack(content: {\n            Image(uiImage: entry.poetry.icon!)\n                .resizable()\n                .frame(width: 70, height: 70)\n            VStack(alignment: .leading, spacing: 4) {\n                Text(entry.poetry.origin)\n                    .font(.system(size: 20))\n                    .fontWeight(.bold)\n                Text(entry.poetry.author)\n                    .font(.system(size: 16))\n                Text(entry.poetry.content)\n                    .font(.system(size: 18))\n            }\n        })\n        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)\n        .padding()\n        .background(LinearGradient(gradient: Gradient(colors: [.init(red: 144 \/ 255.0, green: 252 \/ 255.0, blue: 231 \/ 255.0), .init(red: 50 \/ 204, green: 188 \/ 255.0, blue: 231 \/ 255.0)]), startPoint: .topLeading, endPoint: .bottomTrailing))\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h4>\n\n\n\n<p><a href=\"https:\/\/github.com\/fzhlee\/SwiftUI-Guide#20Image-WebSwitUI-\u5b9e\u73b0URL\u56fe\u7247\u663e\u793a\">https:\/\/github.com\/fzhlee\/SwiftUI-Guide#20Image-WebSwitUI-\u5b9e\u73b0URL\u56fe\u7247\u663e\u793a<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728SwiftUI\u4e2d\uff0c\u901a\u8fc7Image\u6765\u5c55\u793a\u56fe\u7247\uff0c\u4f8b\u5982\uff1a \u4ee5\u4e0a\u90fd\u662f\u4f7f\u7528Image\u52a0\u8f7d\u672c\u5730\u56fe\u7247\u8d44\u6e90\uff0c\u4f46\u662fSwiftU [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1309"}],"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=1309"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1309\/revisions"}],"predecessor-version":[{"id":1313,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1309\/revisions\/1313"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}