{"id":6992,"date":"2025-05-31T14:07:46","date_gmt":"2025-06-05T06:59:46","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6992"},"modified":"2025-06-05T14:07:46","modified_gmt":"2025-06-05T06:59:46","slug":"https-badgameshow-com-steven-172","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-172\/","title":{"rendered":"Swift \u5716\u7247\u683c\u5f0f\u8f49\u63db\u6559\u5b78\uff1a\u5f9e PNG \u8f49\u63db\u6210 JPEG\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, PNG to JPEG, \u5716\u7247\u683c\u5f0f\u8f49\u63db, iOS App \u958b\u767c\"><\/p>\n<h1>Swift \u5716\u7247\u683c\u5f0f\u8f49\u63db\u6559\u5b78\uff1a\u5f9e PNG \u8f49\u63db\u6210 JPEG\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09<\/h1>\n<p>\u5728\u958b\u767c iOS App \u6642\uff0c\u958b\u767c\u8005\u7d93\u5e38\u9700\u8981\u5c07\u5716\u7247\u683c\u5f0f\u5f9e PNG \u8f49\u63db\u6210 JPEG\uff0c\u4ee5\u4fbf\u65bc\u6e1b\u5c11\u6a94\u6848\u5927\u5c0f\u548c\u63d0\u9ad8\u52a0\u8f09\u901f\u5ea6\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift 5 \u4f86\u5b8c\u6210\u9019\u500b\u4efb\u52d9\uff0c\u5305\u62ec\u5b8c\u6574\u7684\u7a0b\u5f0f\u78bc\u793a\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u4f7f\u7528 Swift \u8f49\u63db\u5716\u7247\u683c\u5f0f<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5c07 PNG \u5716\u7247\u8b80\u53d6\u5230 Swift \u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u4f86\u8f09\u5165\u540d\u70ba &#8220;image.png&#8221; \u7684\u5716\u7247\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">guard let image = UIImage(named: \"image.png\") else {\n    print(\"\u7121\u6cd5\u627e\u5230\u5716\u7247\")\n    return\n}\n<\/code><\/pre>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\u5c07\u5716\u7247\u8f49\u63db\u6210 JPEG \u683c\u5f0f\u3002\u6211\u5011\u6703\u4f7f\u7528 `jpegData(compressionQuality:)` \u65b9\u6cd5\uff0c\u4e26\u6307\u5b9a\u58d3\u7e2e\u54c1\u8cea\u70ba 0.8\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">if let data = image.jpegData(compressionQuality: 0.8) {\n    print(\"\u8f49\u63db\u6210\u529f\uff0cJPEG \u8cc7\u6599\u5927\u5c0f\u70ba \\(data.count) bytes\")\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u6aa2\u67e5\u8f49\u63db\u662f\u5426\u6210\u529f\uff0c\u4e26\u4e14\u53ef\u4ee5\u7372\u53d6\u8f49\u63db\u5f8c\u7684 JPEG \u8cc7\u6599\u5927\u5c0f\u3002<\/p>\n<h2>\u5132\u5b58 JPEG \u5716\u7247<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u8f49\u63db\u5f8c\u7684 JPEG \u5716\u7247\u5132\u5b58\u5230\u78c1\u789f\u4e2d\u3002\u4ee5\u4e0b\u662f\u5132\u5b58\u5716\u7247\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">if let data = image.jpegData(compressionQuality: 0.8) {\n    let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first\n    let imageURL = path?.appendingPathComponent(\"image.jpg\")\n    do {\n        try data.write(to: imageURL!)\n        print(\"\u5716\u7247\u5df2\u6210\u529f\u5132\u5b58\u5230 \\(imageURL!)\")\n    } catch {\n        print(\"\u5132\u5b58\u5716\u7247\u6642\u767c\u751f\u932f\u8aa4\uff1a\\(error)\")\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u4f7f\u7528 `write(to:)` \u65b9\u6cd5\u5c07 JPEG \u5716\u7247\u8cc7\u6599\u5beb\u5165\u5230\u6307\u5b9a\u7684\u6a94\u6848\u4e2d\u3002\u5982\u679c\u5132\u5b58\u904e\u7a0b\u4e2d\u51fa\u73fe\u932f\u8aa4\uff0c\u6211\u5011\u5c07\u6355\u7372\u4e26\u6253\u5370\u932f\u8aa4\u4fe1\u606f\u4ee5\u4fbf\u65bc\u6392\u9664\u932f\u8aa4\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u9032\u884c\u5716\u7247\u683c\u5f0f\u8f49\u63db\u548c\u5132\u5b58\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<p>1. **\u7121\u6cd5\u627e\u5230\u5716\u7247**\uff1a\u78ba\u4fdd\u60a8\u7684\u5716\u7247\u540d\u7a31\u6b63\u78ba\u4e14\u4f4d\u65bc\u9069\u7576\u7684\u8cc7\u6e90\u76ee\u9304\u4e2d\u3002<br \/>\n2. **\u5132\u5b58\u5931\u6557**\uff1a\u6aa2\u67e5\u60a8\u7684\u61c9\u7528\u662f\u5426\u6709\u6b0a\u9650\u5beb\u5165\u78c1\u789f\uff0c\u4e26\u78ba\u4fdd\u8def\u5f91\u6b63\u78ba\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u9019\u7a2e\u8f49\u63db\u65b9\u6cd5\u4f86\u8655\u7406\u591a\u7a2e\u5716\u7247\u683c\u5f0f\u6216\u4f7f\u7528\u4e0d\u540c\u7684\u58d3\u7e2e\u54c1\u8cea\u4f86\u512a\u5316\u5716\u7247\u5927\u5c0f\uff0c\u9019\u5c0d\u65bc\u63d0\u9ad8\u61c9\u7528\u6027\u80fd\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u2013-\u8f49\u63db\u5716\u7247\u683c\u5f0f-PNG-to-JPEG\ud83c\udf05_1675400049.371995.webp\" alt=\"Swift \u2013 \u8f49\u63db\u5716\u7247\u683c\u5f0f: PNG to JPEG\ud83c\udf05\" title=\"Swift\u3001\u5716\u7247\u683c\u5f0f\u3001PNG\u3001JPEG\u3001\u8f49\u63db\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u9078\u64c7\u9069\u5408\u7684\u58d3\u7e2e\u54c1\u8cea\uff1f<\/h3>\n<p>\u58d3\u7e2e\u54c1\u8cea\u5728 0 \u5230 1 \u4e4b\u9593\uff0c0 \u8868\u793a\u6700\u4f4e\u54c1\u8cea\uff0c1 \u8868\u793a\u6700\u9ad8\u54c1\u8cea\u3002\u4e00\u822c\u5efa\u8b70\u4f7f\u7528 0.7 \u5230 0.9 \u4e4b\u9593\u7684\u503c\uff0c\u4ee5\u53d6\u5f97\u826f\u597d\u7684\u756b\u8cea\u548c\u6a94\u6848\u5927\u5c0f\u5e73\u8861\u3002<\/p>\n<h3>2. \u6211\u53ef\u4ee5\u5c07\u5176\u4ed6\u683c\u5f0f\u7684\u5716\u7247\u8f49\u63db\u70ba JPEG \u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5c07\u652f\u6301\u7684\u5176\u4ed6\u5716\u7247\u683c\u5f0f\uff08\u5982 BMP\u3001GIF\u3001HEIC \u7b49\uff09\u8f49\u63db\u70ba JPEG\uff0c\u65b9\u6cd5\u985e\u4f3c\uff0c\u53ea\u9700\u8f09\u5165\u4e0d\u540c\u683c\u5f0f\u7684\u5716\u7247\u5373\u53ef\u3002<\/p>\n<h3>3. \u5982\u4f55\u5728\u80cc\u666f\u57f7\u884c\u9019\u500b\u8f49\u63db\u904e\u7a0b\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 GCD\uff08Grand Central Dispatch\uff09\u6216\u64cd\u4f5c\u968a\u5217\u5728\u80cc\u666f\u7dda\u7a0b\u4e2d\u57f7\u884c\u5716\u7247\u8f49\u63db\uff0c\u4ee5\u907f\u514d\u963b\u585e\u4e3b\u7dda\u7a0b\uff0c\u78ba\u4fdd\u61c9\u7528\u754c\u9762\u7684\u6d41\u66a2\u6027\u3002<br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u5feb\u901f\u5c07\u5716\u7247\u683c\u5f0f\u5f9ePNG\u8f49\u63db\u70baJPEG\uff0c\u4e26\u63d0\u4f9b\u7c21\u55ae\u6613\u61c2\u7684\u7a0b\u5f0f\u78bc\u793a\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u5716\u7247\u683c\u5f0f\u8f49\u63db\u70baJPEG\u3002<\/p>\n","protected":false},"author":1,"featured_media":6990,"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-6992","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-\u2013-\u8f49\u63db\u5716\u7247\u683c\u5f0f-PNG-to-JPEG\ud83c\udf05_1675400049.371995.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1OM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6992","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=6992"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6992\/revisions"}],"predecessor-version":[{"id":13605,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6992\/revisions\/13605"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6990"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}