{"id":6884,"date":"2025-06-04T14:40:22","date_gmt":"2025-06-05T07:15:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6884"},"modified":"2025-06-05T14:40:22","modified_gmt":"2025-06-05T07:15:22","slug":"https-badgameshow-com-steven-118","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-118\/","title":{"rendered":"\u4f7f\u7528 Swift \u4e0b\u8f09\u5716\u7247\u7684\u5b8c\u6574\u6307\u5357\u30102025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3011"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u5716\u7247\u4e0b\u8f09, URLSession, UIImage\"><\/p>\n<h2>Swift \u7a0b\u5f0f\u8a9e\u8a00\u6982\u8ff0<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u5feb\u901f\u3001\u5b89\u5168\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u7528\u65bc\u958b\u767c iOS\u3001macOS\u3001watchOS \u548c tvOS \u7b49\u61c9\u7528\u7a0b\u5f0f\u3002\u672c\u6587\u5c07\u6df1\u5165\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u4e0b\u8f09\u5716\u7247\uff0c\u4e26\u63d0\u4f9b\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4f7f\u7528 Swift \u4e0b\u8f09\u5716\u7247\u7684\u6b65\u9a5f<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5229\u7528 <code>URLSession<\/code> \u4f86\u4e0b\u8f09\u5716\u7247\u3002<code>URLSession<\/code> \u662f\u4e00\u500b\u7528\u65bc\u7ba1\u7406\u7db2\u8def\u8acb\u6c42\u7684\u985e\u5225\uff0c\u9069\u5408\u7528\u4f86\u4e0b\u8f09\u5716\u7247\u3001\u6587\u4ef6\u7b49\u8cc7\u6e90\u3002<\/p>\n<h3>1. \u5275\u5efa URLSession \u5be6\u4f8b<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b <code>URLSession<\/code> \u5be6\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let session = URLSession.shared<\/code><\/pre>\n<h3>2. \u5275\u5efa URL \u5be6\u4f8b<\/h3>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b <code>URL<\/code> \u5be6\u4f8b\uff0c\u7528\u65bc\u6307\u5b9a\u8981\u4e0b\u8f09\u7684\u5716\u7247\u7684 URL\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">guard let url = URL(string: \"https:\/\/example.com\/image.jpg\") else {\n    fatalError(\"Invalid URL\")\n}<\/code><\/pre>\n<h3>3. \u5275\u5efa\u6578\u64da\u4efb\u52d9<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>URLSession<\/code> \u7684 <code>dataTask<\/code> \u65b9\u6cd5\u4f86\u5275\u5efa\u4e00\u500b <code>URLSessionDataTask<\/code> \u5be6\u4f8b\uff0c\u7528\u65bc\u4e0b\u8f09\u5716\u7247\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let task = session.dataTask(with: url) { data, response, error in\n    \/\/ \u8655\u7406\u97ff\u61c9\n}<\/code><\/pre>\n<h3>4. \u8655\u7406\u4e0b\u8f09\u7d50\u679c<\/h3>\n<p>\u5728 <code>dataTask<\/code> \u7684\u9589\u5305\u4e2d\uff0c\u6211\u5011\u9700\u8981\u6aa2\u67e5 <code>data<\/code> \u548c <code>error<\/code> \u53c3\u6578\uff0c\u4ee5\u78ba\u4fdd\u4e0b\u8f09\u64cd\u4f5c\u6210\u529f\u5b8c\u6210\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">if let error = error {\n    print(\"\u4e0b\u8f09\u5931\u6557\uff1a\\(error.localizedDescription)\")\n} else if let data = data {\n    \/\/ \u8655\u7406\u6578\u64da\n}<\/code><\/pre>\n<h3>5. \u8f49\u63db\u70ba UIImage<\/h3>\n<p>\u5982\u679c\u4e0b\u8f09\u6210\u529f\uff0c<code>data<\/code> \u53c3\u6578\u5c07\u5305\u542b\u5716\u7247\u6578\u64da\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>UIImage<\/code> \u985e\u5225\u7684 <code>init(data:)<\/code> \u65b9\u6cd5\u5c07\u5176\u8f49\u63db\u70ba <code>UIImage<\/code> \u5be6\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">if let image = UIImage(data: data) {\n    \/\/ \u4f7f\u7528\u5716\u7247\n}<\/code><\/pre>\n<h3>6. \u555f\u52d5\u4e0b\u8f09\u4efb\u52d9<\/h3>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u4f7f\u7528 <code>resume<\/code> \u65b9\u6cd5\u4f86\u555f\u52d5 <code>URLSessionDataTask<\/code> \u5be6\u4f8b\uff0c\u958b\u59cb\u4e0b\u8f09\u5716\u7247\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">task.resume()<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u9032\u884c\u5716\u7247\u4e0b\u8f09\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e7e\u500b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<ul>\n<li><strong>\u7121\u6cd5\u89e3\u6790\u7684 URL\uff1a<\/strong>\u8acb\u78ba\u8a8d\u60a8\u8f38\u5165\u7684 URL \u662f\u6b63\u78ba\u7684\u3002<\/li>\n<li><strong>\u7db2\u7d61\u9023\u63a5\u554f\u984c\uff1a<\/strong>\u78ba\u4fdd\u60a8\u7684\u8a2d\u5099\u5df2\u9023\u63a5\u5230\u7db2\u7d61\u3002<\/li>\n<li><strong>\u6578\u64da\u8f49\u63db\u5931\u6557\uff1a<\/strong>\u4e0b\u8f09\u7684\u6578\u64da\u53ef\u80fd\u4e0d\u662f\u6709\u6548\u7684\u5716\u7247\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u4e0b\u8f09\u5716\u7247\uff0c\u60a8\u9084\u53ef\u4ee5\u9032\u4e00\u6b65\u5b78\u7fd2\u5982\u4f55\u5c07\u4e0b\u8f09\u7684\u5716\u7247\u7de9\u5b58\u5230\u672c\u5730\uff0c\u4ee5\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u3002\u4e86\u89e3 <a href=\"https:\/\/www.raywenderlich.com\/567-swift-image-downloading-and-caching-tutorial\" target=\"_blank\" rel=\"noopener\">Swift \u5716\u7247\u4e0b\u8f09\u8207\u5feb\u53d6\u6559\u5b78<\/a> \u662f\u4e00\u500b\u4e0d\u932f\u7684\u958b\u59cb\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5716\u7247\u4e0b\u8f09-\ud83d\uddbc\ufe0f_1675397610.418709.webp\" alt=\"Swift \u5716\u7247\u4e0b\u8f09 \ud83d\uddbc\ufe0f\" title=\"Swift \u4e0b\u8f09\u5716\u7247, URLSession, URLRequest, UIImage\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u6aa2\u67e5\u5716\u7247\u4e0b\u8f09\u662f\u5426\u6210\u529f\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u5728 <code>dataTask<\/code> \u7684\u9589\u5305\u4e2d\u6aa2\u67e5 <code>error<\/code> \u53c3\u6578\u4f86\u78ba\u5b9a\u4e0b\u8f09\u662f\u5426\u6210\u529f\uff0c\u4e26\u4e14\u53ef\u4ee5\u4f7f\u7528 <code>data<\/code> \u4f86\u78ba\u8a8d\u6578\u64da\u662f\u5426\u6709\u6548\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u8655\u7406\u5927\u578b\u5716\u7247\u4e0b\u8f09\uff1f<\/h3>\n<p>A2: \u5c0d\u65bc\u5927\u578b\u5716\u7247\uff0c\u5efa\u8b70\u4f7f\u7528 <code>URLSession<\/code> \u7684 <code>downloadTask<\/code> \u65b9\u6cd5\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u4e00\u6b21\u52a0\u8f09\u904e\u591a\u6578\u64da\u5c0e\u81f4\u5167\u5b58\u554f\u984c\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728Swift\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528URLSession\u548cURLRequest\u4f86\u4e0b\u8f09\u5716\u7247\uff0c\u4e26\u5c07\u5176\u8f49\u63db\u70baUIImage\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u4e0b\u8f09\u5716\u7247\uff0c\u4e26\u5c07\u5176\u8f49\u63db\u70baUIImage\u3002<\/p>\n","protected":false},"author":1,"featured_media":6883,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6884","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5716\u7247\u4e0b\u8f09-\ud83d\uddbc\ufe0f_1675397610.418709.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1N2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6884"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6884\/revisions"}],"predecessor-version":[{"id":13623,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6884\/revisions\/13623"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6883"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}