{"id":259,"date":"2025-06-01T10:55:31","date_gmt":"2025-06-05T03:16:31","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=259"},"modified":"2025-06-05T10:55:31","modified_gmt":"2025-06-05T03:16:31","slug":"259","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/259\/","title":{"rendered":"Swift 5 &#8211; \u5b8c\u6574\u6307\u5357\uff1a\u5982\u4f55\u958b\u555f\u76f8\u6a5f\u53ca\u76f8\u7c3f\u6b0a\u9650\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c\u4f7f\u7528\u8005\u7684\u76f8\u6a5f\u548c\u76f8\u7c3f\u6b0a\u9650\u662f\u5fc5\u9808\u6b63\u78ba\u8a2d\u7f6e\u7684\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u63d0\u4f9b 2025 \u5e74\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u9806\u5229\u958b\u555f\u76f8\u6a5f\u8207\u76f8\u7c3f\u6b0a\u9650\uff0c\u4e26\u5728\u5fc5\u8981\u6642\u5f15\u5c0e\u7528\u6236\u8df3\u8f49\u81f3\u8a2d\u5b9a\u9801\u9762\u3002<\/p>\n<p>### 1. Xcode \u8a2d\u7f6e<\/p>\n<p>\u5728\u4f60\u7684 Xcode \u5c08\u6848\u4e2d\uff0c\u5fc5\u9808\u5728 Info.plist \u6587\u4ef6\u4e2d\u52a0\u5165\u76f8\u6a5f\u548c\u76f8\u7c3f\u7684\u4f7f\u7528\u8aaa\u660e\u3002\u9019\u4e9b\u8aaa\u660e\u6703\u5728\u7528\u6236\u9996\u6b21\u8acb\u6c42\u6b0a\u9650\u6642\u986f\u793a\u3002<\/p>\n<p>&#8220;`xml<br \/>\n<key>NSCameraUsageDescription<\/key><br \/>\n<string>\u6b64\u61c9\u7528\u7a0b\u5f0f\u9700\u8981\u8a2a\u554f\u76f8\u6a5f\u4ee5\u62cd\u651d\u7167\u7247\u3002<\/string><br \/>\n<key>NSPhotoLibraryUsageDescription<\/key><br \/>\n<string>\u6b64\u61c9\u7528\u7a0b\u5f0f\u9700\u8981\u8a2a\u554f\u76f8\u7c3f\u4ee5\u5132\u5b58\u7167\u7247\u3002<\/string><br \/>\n&#8220;`<\/p>\n<p>![Xcode Info.plist \u8a2d\u7f6e](https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/09\/wp_editor_md_bbd56584ac3d0ab6bd1c90d33d3e52e6.jpg)<\/p>\n<p>### 2. \u6b0a\u9650\u6388\u6b0a\u51fd\u5f0f<\/p>\n<p>\u4e0b\u9762\u7684 Swift \u51fd\u5f0f\u5c07\u6aa2\u67e5\u76f8\u6a5f\u548c\u76f8\u7c3f\u7684\u6388\u6b0a\u72c0\u614b\uff0c\u4e26\u76f8\u61c9\u5730\u8acb\u6c42\u7528\u6236\u6388\u6b0a\u3002<\/p>\n<p>&#8220;`swift<br \/>\nimport Photos<br \/>\nimport AVFoundation<\/p>\n<p>func authorize() -> Bool {<br \/>\n    let photoLibraryStatus = PHPhotoLibrary.authorizationStatus() \/\/ \u76f8\u7c3f\u8acb\u6c42<br \/>\n    let camStatus = AVCaptureDevice.authorizationStatus(for: .video) \/\/ \u76f8\u6a5f\u8acb\u6c42<br \/>\n    switch (camStatus, photoLibraryStatus) {<br \/>\n    case (.authorized, .authorized): \/\/ \u5169\u500b\u90fd\u5141\u8a31<br \/>\n        return true<br \/>\n    case (.notDetermined, .notDetermined): \/\/ \u5169\u500b\u90fd\u672a\u6c7a\u5b9a\uff0c\u8acb\u6c42\u6388\u6b0a<br \/>\n        AVCaptureDevice.requestAccess(for: .video) { _ in<br \/>\n            self.authorize()<br \/>\n        }<br \/>\n        PHPhotoLibrary.requestAuthorization { _ in<br \/>\n            self.authorize()<br \/>\n        }<br \/>\n    case (.authorized, .notDetermined): \/\/ \u76f8\u6a5f\u5df2\u6388\u6b0a\uff0c\u76f8\u7c3f\u672a\u6c7a\u5b9a<br \/>\n        PHPhotoLibrary.requestAuthorization { _ in<br \/>\n            self.authorize()<br \/>\n        }<br \/>\n    case (.authorized, .denied): \/\/ \u76f8\u6a5f\u5df2\u6388\u6b0a\uff0c\u76f8\u7c3f\u88ab\u62d2\u7d55\uff0c\u63d0\u793a\u7528\u6236<br \/>\n        showAlertForSettings()<br \/>\n    default: \/\/ \u5176\u4ed6\u60c5\u6cc1<br \/>\n        showAlertForSettings()<br \/>\n    }<br \/>\n    return false<br \/>\n}<\/p>\n<p>func showAlertForSettings() {<br \/>\n    let alertController = UIAlertController(title: &#8220;\u6b0a\u9650\u63d0\u9192&#8221;, message: &#8220;\u8acb\u5141\u8a31\u61c9\u7528\u7a0b\u5f0f\u8a2a\u554f\u76f8\u6a5f\u548c\u76f8\u7c3f\u3002\u524d\u5f80\u8a2d\u5b9a\u9801\u9762\u4fee\u6539\u6b0a\u9650\uff1f&#8221;, preferredStyle: .alert)<br \/>\n    let cancelAction = UIAlertAction(title: &#8220;\u53d6\u6d88&#8221;, style: .cancel, handler: nil)<br \/>\n    let settingsAction = UIAlertAction(title: &#8220;\u8a2d\u5b9a&#8221;, style: .default) { _ in<br \/>\n        if let url = URL(string: UIApplication.openSettingsURLString) {<br \/>\n            UIApplication.shared.open(url, options: [:], completionHandler: nil)<br \/>\n        }<br \/>\n    }<br \/>\n    alertController.addAction(cancelAction)<br \/>\n    alertController.addAction(settingsAction)<br \/>\n    present(alertController, animated: true, completion: nil)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### 3. \u5728 ViewController \u4e2d\u4f7f\u7528<\/p>\n<p>\u5728 `viewDidLoad` \u51fd\u5f0f\u4e2d\u8abf\u7528 `authorize()` \u4f86\u6aa2\u67e5\u6b0a\u9650\u72c0\u614b\u3002<\/p>\n<p>&#8220;`swift<br \/>\noverride func viewDidLoad() {<br \/>\n    super.viewDidLoad()<br \/>\n    if authorize() {<br \/>\n        print(&#8220;\u958b\u59cb\u4f7f\u7528\u76f8\u6a5f\u548c\u76f8\u7c3f&#8221;)<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### 4. \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u4f7f\u7528\u76f8\u6a5f\u548c\u76f8\u7c3f\uff1a<\/p>\n<p>![Swift Demo](https:\/\/i.imgur.com\/Rsrt0jB.gif)<\/p>\n<p>### 5. \u932f\u8aa4\u6392\u9664<\/p>\n<p>&#8211; \u78ba\u4fdd\u5728 Info.plist \u4e2d\u6b63\u78ba\u8a2d\u7f6e\u4e86\u76f8\u6a5f\u548c\u76f8\u7c3f\u7684\u4f7f\u7528\u8aaa\u660e\u3002<br \/>\n&#8211; \u6aa2\u67e5\u662f\u5426\u5728\u5be6\u6a5f\u4e0a\u6e2c\u8a66\uff0c\u6a21\u64ec\u5668\u7121\u6cd5\u4f7f\u7528\u76f8\u6a5f\u529f\u80fd\u3002<br \/>\n&#8211; \u5982\u679c\u7528\u6236\u62d2\u7d55\u4e86\u6b0a\u9650\uff0c\u61c9\u8a72\u63d0\u4f9b\u5f15\u5c0e\u4fe1\u606f\uff0c\u544a\u8a34\u4ed6\u5011\u5982\u4f55\u4fee\u6539\u8a2d\u5b9a\u3002<\/p>\n<p>### 6. \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u76f8\u6a5f\u548c\u76f8\u7c3f\u6b0a\u9650\u5916\uff0c\u9084\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u5176\u4ed6 API\uff0c\u5982 Core Image \u6216 AVFoundation \u4f86\u9032\u884c\u9032\u968e\u5716\u50cf\u8655\u7406\u548c\u62cd\u651d\u529f\u80fd\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: \u70ba\u4ec0\u9ebc\u9700\u8981\u8acb\u6c42\u76f8\u6a5f\u548c\u76f8\u7c3f\u7684\u6b0a\u9650\uff1f**<br \/>\nA1: \u5728 iOS \u4e2d\uff0c\u51fa\u65bc\u7528\u6236\u96b1\u79c1\u7684\u8003\u91cf\uff0c\u61c9\u7528\u7a0b\u5f0f\u5fc5\u9808\u986f\u5f0f\u8acb\u6c42\u4f7f\u7528\u76f8\u6a5f\u548c\u76f8\u7c3f\u7684\u6b0a\u9650\u3002<\/p>\n<p>**Q2: \u5982\u679c\u7528\u6236\u62d2\u7d55\u4e86\u6b0a\u9650\uff0c\u6211\u8a72\u600e\u9ebc\u8fa6\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u5411\u7528\u6236\u986f\u793a\u4e00\u500b\u63d0\u793a\uff0c\u544a\u8a34\u4ed6\u5011\u5982\u4f55\u5728\u8a2d\u5b9a\u4e2d\u6253\u958b\u6b0a\u9650\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u6aa2\u67e5\u7528\u6236\u7684\u6388\u6b0a\u72c0\u614b\uff1f**<br \/>\nA3: \u53ef\u4ee5\u4f7f\u7528 `PHPhotoLibrary.authorizationStatus()` \u548c `AVCaptureDevice.authorizationStatus(for:)` \u4f86\u6aa2\u67e5\u76f8\u7c3f\u548c\u76f8\u6a5f\u7684\u6388\u6b0a\u72c0\u614b\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u5167\u5bb9\u4e0d\u50c5\u66f4\u65b0\u4e86\u904e\u6642\u7684\u8a9e\u6cd5\uff0c\u9084\u63d0\u4f9b\u4e86\u8a73\u7d30\u7684\u6559\u5b78\u6d41\u7a0b\u8207\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u4e26\u7b26\u5408 SEO \u6700\u4f73\u5be6\u8e10\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8a2d\u7f6e\u4f7f\u7528\u8005\u624b\u6a5f\u76f8\u6a5f\u3001\u76f8\u7c3f\u6b0a\u9650\u958b\u555f\uff0c\u70ba\u958b\u555f\u81ea\u52d5\u8df3\u8f49\u8a2d\u5b9a\u9801\u9762<br \/>\nXcode\u8a2d\u7f6e\u5230\u7a0b\u5f0f\u64b0\u5beb\u6309\u8868\u64cd\u8ab2\u4fdd\u8b49\u6210\u529f<br \/>\n&#8211; PHPhotoLibrary \u76f8\u7c3f\u6b0a\u9650<br \/>\n&#8211; AVCaptureDevice \u76f8\u6a5f\u6b0a\u9650<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,108],"tags":[55,54,53,52],"class_list":["post-259","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","category-xcode","tag-avcapturedevice","tag-phphotolibrary","tag-53","tag-52"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/scFK27-259","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/259","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=259"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":7565,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/259\/revisions\/7565"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}