{"id":6792,"date":"2025-06-03T12:32:32","date_gmt":"2025-06-05T05:29:32","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6792"},"modified":"2025-06-05T12:32:32","modified_gmt":"2025-06-05T05:29:32","slug":"https-badgameshow-com-steven-72","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-72\/","title":{"rendered":"2025 \u6700\u65b0 Swift \u76f8\u6a5f\u529f\u80fd\u958b\u767c\u6559\u5b78 \ud83d\udcf7 | \u5b8c\u6574\u5be6\u4f5c\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u76f8\u6a5f\u529f\u80fd, \u4f7f\u7528, AVCaptureSession, \u76f8\u6a5f\u958b\u767c, iOS\u958b\u767c\"><\/p>\n<h1>2025 \u6700\u65b0 Swift \u76f8\u6a5f\u529f\u80fd\u958b\u767c\u6559\u5b78 \ud83d\udcf7<\/h1>\n<p>\u96a8\u8457\u667a\u6167\u578b\u624b\u6a5f\u7684\u666e\u53ca\uff0c\u76f8\u6a5f\u529f\u80fd\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Swift \u662f\u4e00\u7a2e\u7528\u65bc iOS \u548c macOS \u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u80fd\u5920\u8b93\u958b\u767c\u8005\u8f15\u9b06\u958b\u767c\u51fa\u76f8\u6a5f\u529f\u80fd\u7684\u61c9\u7528\u7a0b\u5f0f\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u4f86\u958b\u767c\u76f8\u6a5f\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u52a9\u4f60\u8f15\u9b06\u958b\u767c\u81ea\u5df1\u7684\u76f8\u6a5f\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e00\uff1a\u8a2d\u5b9a Xcode \u5c08\u6848<\/h2>\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684 Xcode \u5c08\u6848\u4e2d\u52a0\u5165 <strong>AVFoundation<\/strong> \u6846\u67b6\u3002\u53ef\u4ee5\u5728\u5c08\u6848\u7684 <code>General<\/code> \u6a19\u7c64\u4e0b\u7684 <code>Frameworks, Libraries, and Embedded Content<\/code> \u5340\u57df\u4e2d\u6dfb\u52a0\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e8c\uff1a\u914d\u7f6e AVCaptureSession<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b <strong>AVCaptureSession<\/strong> \u7269\u4ef6\uff0c\u9019\u5c07\u7528\u65bc\u6355\u7372\u548c\u7de8\u8f2f\u97f3\u8a0a\u53ca\u8996\u983b\u3002<\/p>\n<h2>\u6b65\u9a5f\u4e09\uff1a\u6dfb\u52a0 AVCaptureDevice<\/h2>\n<p>\u7136\u5f8c\uff0c\u8a2d\u7f6e\u4e00\u500b <strong>AVCaptureDevice<\/strong> \u7269\u4ef6\uff0c\u9019\u662f\u5f9e\u8a2d\u5099\u4e2d\u7372\u53d6\u8996\u983b\u6d41\u7684\u95dc\u9375\u3002<\/p>\n<h2>\u6b65\u9a5f\u56db\uff1a\u8a2d\u7f6e\u9810\u89bd\u5c64<\/h2>\n<p>\u6700\u5f8c\uff0c\u4f7f\u7528 <strong>AVCaptureVideoPreviewLayer<\/strong> \u4f86\u986f\u793a\u8996\u983b\u756b\u9762\u3002<\/p>\n<h2>\u5b8c\u6574 Swift \u7bc4\u4f8b<\/h2>\n<pre><code class=\"language-swift line-numbers\">import UIKit\nimport AVFoundation\n\nclass CameraViewController: UIViewController {\n    let captureSession = AVCaptureSession()\n    var previewLayer: AVCaptureVideoPreviewLayer!\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        setupCamera()\n    }\n\n    func setupCamera() {\n        guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }\n        do {\n            let input = try AVCaptureDeviceInput(device: captureDevice)\n            captureSession.addInput(input)\n\n            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)\n            previewLayer.frame = view.layer.bounds\n            previewLayer.videoGravity = .resizeAspectFill\n            view.layer.addSublayer(previewLayer)\n\n            captureSession.startRunning()\n        } catch {\n            print(\"Error setting up camera: \\(error)\")\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u5e6b\u52a9\u4f60\u5feb\u901f\u5efa\u7acb\u4e00\u500b\u7c21\u55ae\u7684\u76f8\u6a5f\u61c9\u7528\u3002\u9032\u4e00\u6b65\u7684\u932f\u8aa4\u8655\u7406\u548c\u529f\u80fd\u64f4\u5c55\u5c07\u4f7f\u4f60\u7684\u61c9\u7528\u66f4\u70ba\u5065\u5168\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<ul>\n<li><strong>\u7121\u6cd5\u8a2a\u554f\u76f8\u6a5f\uff1a<\/strong>\u78ba\u4fdd\u4f60\u7684\u61c9\u7528\u6709\u76f8\u6a5f\u7684\u6b0a\u9650\u3002\u5728 <code>Info.plist<\/code> \u4e2d\u6dfb\u52a0 <code>NSCameraUsageDescription<\/code> \u9375\u3002<\/li>\n<li><strong>\u756b\u9762\u4e0d\u986f\u793a\uff1a<\/strong>\u6aa2\u67e5 <code>AVCaptureVideoPreviewLayer<\/code> \u7684\u914d\u7f6e\uff0c\u78ba\u4fdd\u5b83\u5df2\u6b63\u78ba\u5730\u6dfb\u52a0\u5230\u8996\u5716\u5c64\u7d1a\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f60\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u8b93\u61c9\u7528\u652f\u63f4\u62cd\u7167\u3001\u9304\u5f71\u3001\u6216\u662f\u5373\u6642\u8655\u7406\u5f71\u50cf\uff0c\u4f8b\u5982\u52a0\u5165\u6ffe\u93e1\u6216\u5be6\u73fe QR \u78bc\u6383\u63cf\u529f\u80fd\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u76f8\u6a5f\u529f\u80fd\u4f7f\u7528-\ud83d\udcf7_1675395836.914068.webp\" alt=\"Swift \u76f8\u6a5f\u529f\u80fd\u4f7f\u7528 \ud83d\udcf7\" title=\"Swift\u76f8\u6a5f\u529f\u80fd, Swift\u64cd\u4f5c\u76f8\u6a5f, Swift\u8a9e\u8a00\u61c9\u7528, \u76f8\u6a5f\u529f\u80fd\u5275\u5efa\u61c9\u7528, Swift\u76f8\u6a5f\u64cd\u4f5c\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5728 Swift \u4e2d\u8acb\u6c42\u76f8\u6a5f\u4f7f\u7528\u6b0a\u9650\uff1f<\/h3>\n<p>\u5728 <code>Info.plist<\/code> \u4e2d\u6dfb\u52a0 <code>NSCameraUsageDescription<\/code> \u9375\uff0c\u4e26\u63d0\u4f9b\u4f7f\u7528\u8aaa\u660e\u6587\u5b57\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728\u76f8\u6a5f\u61c9\u7528\u4e2d\u5be6\u73fe\u62cd\u7167\u529f\u80fd\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>AVCapturePhotoOutput<\/code> \u4f86\u5be6\u73fe\u62cd\u7167\u529f\u80fd\uff0c\u4e26\u5c07\u6355\u7372\u7684\u5f71\u50cf\u4fdd\u5b58\u5230\u76f8\u518a\u4e2d\u3002<\/p>\n<h3>3. \u5982\u4f55\u8655\u7406\u76f8\u6a5f\u9810\u89bd\u7684\u65b9\u5411\u554f\u984c\uff1f<\/h3>\n<p>\u53ef\u4ee5\u6839\u64da\u8a2d\u5099\u65b9\u5411\u8abf\u6574 <code>AVCaptureVideoPreviewLayer<\/code> \u7684 <code>connection<\/code> \u7684 <code>videoOrientation<\/code> \u5c6c\u6027\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4e2d\u7684\u76f8\u6a5f\u529f\u80fd\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u64cd\u4f5c\u76f8\u6a5f\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u76f8\u6a5f\u529f\u80fd\u4f86\u5275\u5efa\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":6790,"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-6792","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-\u76f8\u6a5f\u529f\u80fd\u4f7f\u7528-\ud83d\udcf7_1675395836.914068.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Ly","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6792","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=6792"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6792\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6790"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}