{"id":6723,"date":"2025-05-29T13:05:41","date_gmt":"2025-06-05T05:05:40","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6723"},"modified":"2025-06-05T13:05:41","modified_gmt":"2025-06-05T05:05:40","slug":"https-badgameshow-com-steven-39","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-39\/","title":{"rendered":"Swift \u62cd\u7167\u529f\u80fd\u5b8c\u6574\u6559\u5b78\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09\ud83d\udcf7"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u62cd\u7167\u529f\u80fd, UIImagePickerController, iOS\"><\/p>\n<h2>Swift \u62cd\u7167\u529f\u80fd\u5b8c\u6574\u6559\u5b78\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09\ud83d\udcf7<\/h2>\n<p>Swift \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u958b\u767c\u51fa\u529f\u80fd\u5f37\u5927\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u5728 iOS \u4e0a\uff0cSwift \u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u5be6\u73fe\u62cd\u7167\u529f\u80fd\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u96a8\u6642\u62cd\u651d\u4e26\u5206\u4eab\u7167\u7247\u3002<\/p>\n<h3>\u4f7f\u7528 UIImagePickerController \u5be6\u73fe\u62cd\u7167\u529f\u80fd<\/h3>\n<p>\u5728\u958b\u767c\u62cd\u7167\u529f\u80fd\u6642\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 Swift \u7684 <strong>UIImagePickerController<\/strong> \u985e\u5225\u4f86\u5be6\u73fe\u3002\u9019\u500b\u985e\u5225\u5141\u8a31\u958b\u767c\u8005\u8f15\u9b06\u5730\u5f9e\u76f8\u6a5f\u6216\u76f8\u7c3f\u4e2d\u9078\u64c7\u7167\u7247\uff0c\u4e26\u4e14\u652f\u6301\u5373\u6642\u62cd\u651d\u529f\u80fd\u3002<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u8a2d\u5b9a UIImagePickerController<\/h3>\n<p>\u70ba\u4e86\u4f7f\u7528 UIImagePickerController\uff0c\u958b\u767c\u8005\u9700\u8981\u9996\u5148\u5728 ViewController \u4e2d\u521d\u59cb\u5316\u5b83\uff0c\u4e26\u8a2d\u5b9a\u5fc5\u8981\u7684\u5c6c\u6027\uff0c\u5982 delegate \u548c sourceType\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let imagePicker = UIImagePickerController()\nimagePicker.delegate = self\nimagePicker.sourceType = .camera \/\/ \u6216\u8005 .photoLibrary \u4f86\u9078\u64c7\u76f8\u7c3f\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 2\uff1a\u986f\u793a UIImagePickerController<\/h3>\n<p>\u63a5\u8457\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5c07 UIImagePickerController \u986f\u793a\u51fa\u4f86\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u958b\u59cb\u62cd\u651d\u7167\u7247\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">present(imagePicker, animated: true, completion: nil)\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 3\uff1a\u8655\u7406\u62cd\u651d\u7684\u7167\u7247<\/h3>\n<p>\u7576\u4f7f\u7528\u8005\u62cd\u651d\u5b8c\u7167\u7247\u5f8c\uff0cUIImagePickerController \u6703\u89f8\u767c\u5176 delegate \u65b9\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u53d6\u5f97\u4f7f\u7528\u8005\u62cd\u651d\u7684\u7167\u7247\u4e26\u9032\u884c\u5f8c\u7e8c\u8655\u7406\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {\n    if let image = info[.originalImage] as? UIImage {\n        \/\/ \u53d6\u5f97\u4f7f\u7528\u8005\u62cd\u651d\u7684\u7167\u7247\uff0c\u4e26\u53ef\u9032\u884c\u5f8c\u7e8c\u8655\u7406\uff08\u5982\u5132\u5b58\u6216\u986f\u793a\uff09\n    }\n    dismiss(animated: true, completion: nil)\n}\n<\/code><\/pre>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u5728\u5be6\u4f5c\u62cd\u7167\u529f\u80fd\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u672a\u7372\u5f97\u76f8\u6a5f\u5b58\u53d6\u6b0a\u9650\u6703\u5c0e\u81f4\u61c9\u7528\u7a0b\u5f0f\u5d29\u6f70\u3002\u78ba\u8a8d\u5728 Info.plist \u4e2d\u52a0\u5165\u5fc5\u8981\u7684\u6b0a\u9650\u8a2d\u5b9a\uff1a<\/p>\n<pre><code>NSCameraUsageDescription: \"\u6b64\u61c9\u7528\u7a0b\u5f0f\u9700\u8981\u5b58\u53d6\u76f8\u6a5f\u4ee5\u62cd\u651d\u7167\u7247\"\n<\/code><\/pre>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u62cd\u7167\u529f\u80fd\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u7d50\u5408\u5176\u4ed6 API \u4f86\u589e\u5f37\u61c9\u7528\u7a0b\u5f0f\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <strong>AVFoundation<\/strong> \u4f86\u5be6\u73fe\u66f4\u9ad8\u7d1a\u7684\u76f8\u6a5f\u63a7\u5236\u6216\u5f71\u50cf\u8655\u7406\u3002<\/p>\n<p>\u4f7f\u7528 Swift \u4f86\u958b\u767c\u62cd\u7167\u529f\u80fd\u975e\u5e38\u7c21\u55ae\uff0c\u5e7e\u884c\u7a0b\u5f0f\u78bc\u5c31\u53ef\u4ee5\u8b93\u4f7f\u7528\u8005\u8f15\u9b06\u5730\u62cd\u651d\u4e26\u8655\u7406\u7167\u7247\uff0c\u9032\u4e00\u6b65\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u4f7f\u7528\u9ad4\u9a57\u3002<\/p>\n<h3>\u53c3\u8003\u5716\u7247<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u62cd\u7167\u529f\u80fd-\ud83d\udcf7_1675394614.6874728.webp\" alt=\"Swift \u62cd\u7167\u529f\u80fd \ud83d\udcf7\" title=\"Swift\u62cd\u7167, UIImagePickerController, AVFoundation, \u62cd\u7167\u529f\u80fd, \u62cd\u651d\u7167\u7247\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: \u5982\u4f55\u5728 Swift \u4e2d\u6aa2\u67e5\u76f8\u6a5f\u5b58\u53d6\u6b0a\u9650\uff1f<\/h3>\n<p>A1: \u53ef\u4ee5\u4f7f\u7528 <strong>AVCaptureDevice.authorizationStatus(for: .video)<\/strong> \u4f86\u6aa2\u67e5\u76f8\u6a5f\u7684\u5b58\u53d6\u6b0a\u9650\uff0c\u4e26\u6839\u64da\u7d50\u679c\u8acb\u6c42\u5b58\u53d6\u6b0a\u9650\u3002<\/p>\n<h3>Q2: UIImagePickerController \u662f\u5426\u652f\u6301\u5f71\u7247\u62cd\u651d\uff1f<\/h3>\n<p>A2: \u662f\u7684\uff0cUIImagePickerController \u4e5f\u652f\u6301\u5f71\u7247\u62cd\u651d\uff0c\u60a8\u53ea\u9700\u5c07 <strong>sourceType<\/strong> \u8a2d\u70ba <strong>.camera<\/strong>\uff0c\u4e26\u8a2d\u7f6e <strong>mediaTypes<\/strong> \u5c6c\u6027\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5c07\u62cd\u651d\u7684\u7167\u7247\u5132\u5b58\u5230\u76f8\u7c3f\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\u4f7f\u7528 <strong>UIImageWriteToSavedPhotosAlbum<\/strong> \u51fd\u6578\u5c07 UIImage \u5beb\u5165 iOS \u7684\u76f8\u7c3f\u4e2d\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u5be6\u73fe\u62cd\u7167\u529f\u80fd\uff0c\u4e26\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528UIImagePickerController\u4f86\u62cd\u7167\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528AVFoundation\u4f86\u62cd\u651d\u9ad8\u8cea\u91cf\u7684\u7167\u7247\u3002<\/p>\n","protected":false},"author":1,"featured_media":6721,"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-6723","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-\u62cd\u7167\u529f\u80fd-\ud83d\udcf7_1675394614.6874728.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Kr","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6723","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=6723"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6723\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6721"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}