{"id":7750,"date":"2025-05-30T14:29:22","date_gmt":"2025-06-05T06:40:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7750"},"modified":"2025-06-05T14:29:22","modified_gmt":"2025-06-05T06:40:22","slug":"https-badgameshow-com-steven-518","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-518\/","title":{"rendered":"SwiftUI \u7167\u7247\u5eab\u5b8c\u5168\u6307\u5357\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, SwiftUI, \u7167\u7247\u5eab, UIImagePickerController, 2025\"><\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0cSwiftUI \u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\u4f86\u8f15\u9b06\u8655\u7406\u7167\u7247\u5eab\u529f\u80fd\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 SwiftUI \u4e2d\u7684\u7167\u7247\u5eab\uff0c\u4e26\u5c55\u793a\u5982\u4f55\u5c07\u7167\u7247\u8f15\u9b06\u52a0\u5165\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u4f7f\u7528 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5efa\u7acb ImagePicker<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 SwiftUI \u4e2d\u5b9a\u7fa9\u4e00\u500b <strong>ImagePicker<\/strong> \u7269\u4ef6\uff0c\u4e26\u5c07\u5176\u521d\u59cb\u5316\u70ba\u4e00\u500b <strong>UIImagePickerController<\/strong> \u7269\u4ef6\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u5be6\u4f5c\u6b65\u9a5f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">import SwiftUI\nimport UIKit\n\nstruct ImagePicker: UIViewControllerRepresentable {\n    @Binding var isShown: Bool\n    @Binding var image: UIImage?\n\n    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {\n        let picker = UIImagePickerController()\n        picker.sourceType = .photoLibrary\n        picker.delegate = context.coordinator\n        return picker\n    }\n\n    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {}\n\n    func makeCoordinator() -> Coordinator {\n        return Coordinator(isShown: <span class=\"katex math inline\">isShown, image:<\/span>image)\n    }\n\n    class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {\n        @Binding var isShown: Bool\n        @Binding var image: UIImage?\n\n        init(isShown: Binding<Bool>, image: Binding<UIImage?>) {\n            _isShown = isShown\n            _image = image\n        }\n\n        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {\n            if let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {\n                self.image = selectedImage\n            }\n            isShown = false\n        }\n\n        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {\n            isShown = false\n        }\n    }\n}\n<\/code><\/pre>\n<h2>\u5728 SwiftUI \u4e2d\u4f7f\u7528 ImagePicker<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u5728 SwiftUI \u4e2d\u4f7f\u7528 <strong>ImagePicker<\/strong> \u7269\u4ef6\u4f86\u986f\u793a\u7167\u7247\u5eab\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u6309\u9215\u4f86\u89f8\u767c\u7167\u7247\u9078\u64c7\u5668\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">struct ContentView: View {\n    @State private var isShown = false\n    @State private var selectedImage: UIImage?\n\n    var body: some View {\n        VStack {\n            Button(action: {\n                self.isShown = true\n            }) {\n                Text(\"\u9078\u64c7\u7167\u7247\")\n            }\n            .sheet(isPresented: <span class=\"katex math inline\">isShown, onDismiss: loadImage) {\n                ImagePicker(isShown: self.<\/span>isShown, image: self.$selectedImage)\n            }\n\n            if let image = selectedImage {\n                Image(uiImage: image)\n                    .resizable()\n                    .scaledToFit()\n                    .frame(width: 300, height: 300)\n            }\n        }\n    }\n\n    func loadImage() {\n        \/\/ \u5728\u6b64\u8655\u6dfb\u52a0\u4efb\u4f55\u8207\u5716\u50cf\u52a0\u8f09\u76f8\u95dc\u7684\u8655\u7406\u908f\u8f2f\n    }\n}\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 <strong>UIImagePickerController<\/strong> \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\u672a\u6b63\u78ba\u8a2d\u7f6e delegate \u6216\u672a\u8acb\u6c42\u76f8\u6a5f\u6216\u7167\u7247\u5eab\u7684\u6b0a\u9650\u3002\u8acb\u78ba\u4fdd\u5728\u4f7f\u7528\u524d\u5df2\u5728 <code>Info.plist<\/code> \u4e2d\u6dfb\u52a0\u5fc5\u8981\u7684\u6b0a\u9650\u63cf\u8ff0\uff1a<\/p>\n<ul>\n<li><code>NSPhotoLibraryUsageDescription<\/code> &#8211; \u7528\u65bc\u8a2a\u554f\u7167\u7247\u5eab\u7684\u63cf\u8ff0\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u60a8\u53ef\u4ee5\u9032\u4e00\u6b65\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u5716\u50cf\u8655\u7406\u529f\u80fd\uff0c\u6216\u5c07\u5716\u50cf\u4e0a\u50b3\u5230\u4f3a\u670d\u5668\u3002\u9019\u4e9b\u90fd\u662f\u5e38\u898b\u7684\u61c9\u7528\u5834\u666f\uff0c\u80fd\u8b93\u60a8\u7684\u61c9\u7528\u66f4\u5177\u5438\u5f15\u529b\u548c\u529f\u80fd\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/SwiftUI-\u4e2d\u7684\u7167\u7247\u5eab_1676963333.762282.webp\" alt=\"SwiftUI \u4e2d\u7684\u7167\u7247\u5eab\" title=\"SwiftUI, \u7167\u7247\u5eab, \u7a0b\u5f0f\u78bc, \u7bc4\u4f8b, \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\u8acb\u6c42\u7167\u7247\u5eab\u7684\u8a2a\u554f\u6b0a\u9650\uff1f<\/h3>\n<p>\u60a8\u9700\u8981\u5728\u61c9\u7528\u7684 <code>Info.plist<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0 <code>NSPhotoLibraryUsageDescription<\/code> \u9375\uff0c\u4e26\u63d0\u4f9b\u4e00\u500b\u63cf\u8ff0\uff0c\u9019\u6a23\u4f7f\u7528\u8005\u5728\u9996\u6b21\u4f7f\u7528\u6642\u6703\u770b\u5230\u8a72\u63d0\u793a\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406\u9078\u64c7\u7684\u5716\u7247\uff1f<\/h3>\n<p>\u60a8\u53ef\u4ee5\u5728 <code>imagePickerController(_:didFinishPickingMediaWithInfo:)<\/code> \u65b9\u6cd5\u4e2d\u7372\u53d6\u9078\u64c7\u7684\u5716\u7247\uff0c\u4e26\u901a\u904e <code>Binding<\/code> \u5c07\u5176\u5132\u5b58\u5230\u72c0\u614b\u8b8a\u6578\u4e2d\u3002<\/p>\n<h3>3. \u4f7f\u7528\u6b64\u529f\u80fd\u6642\u9700\u8981\u6ce8\u610f\u4ec0\u9ebc\uff1f<\/h3>\n<p>\u9664\u4e86\u78ba\u4fdd\u8acb\u6c42\u6b63\u78ba\u7684\u6b0a\u9650\u5916\uff0c\u9084\u8981\u6ce8\u610f\u4f7f\u7528\u8005\u7684\u96b1\u79c1\u548c\u6578\u64da\u5b89\u5168\uff0c\u76e1\u91cf\u907f\u514d\u5728\u672a\u7d93\u5141\u8a31\u7684\u60c5\u6cc1\u4e0b\u5b58\u5132\u6216\u50b3\u8f38\u4f7f\u7528\u8005\u7684\u5716\u7247\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39SwiftUI\u4e2d\u7684\u7167\u7247\u5eab\u529f\u80fd\uff0c\u8a73\u7d30\u89e3\u91cb\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u64cd\u4f5c\u7167\u7247\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\u4f86\u8b93\u5b78\u751f\u5011\u66f4\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n","protected":false},"author":1,"featured_media":7749,"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-7750","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\/SwiftUI-\u4e2d\u7684\u7167\u7247\u5eab_1676963333.762282.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-210","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7750","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=7750"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7750\/revisions"}],"predecessor-version":[{"id":13110,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7750\/revisions\/13110"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7749"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}