{"id":7022,"date":"2025-06-04T14:19:51","date_gmt":"2025-06-05T06:58:50","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=7022"},"modified":"2025-06-05T14:19:51","modified_gmt":"2025-06-05T06:58:50","slug":"https-badgameshow-com-steven-187","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-187\/","title":{"rendered":"Swift 2025\uff1a\u9078\u64c7\u7167\u7247\u4e26\u9032\u884c\u88c1\u5207\u7684\u5b8c\u6574\u6559\u5b78 \ud83d\udcf7"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift,\u9078\u64c7\u7167\u7247,\u88c1\u5207\u7167\u7247,UIImagePickerController,CropViewController\"><\/p>\n<h2>Swift 2025\uff1a\u9078\u64c7\u7167\u7247\u4e26\u9032\u884c\u88c1\u5207\u7684\u5b8c\u6574\u6559\u5b78 \ud83d\udcf7<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>UIImagePickerController<\/strong> \u4f86\u9078\u64c7\u7167\u7247\uff0c\u4e26\u4e14\u53ef\u4ee5\u5c0d\u7167\u7247\u9032\u884c\u88c1\u5207\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u4f86\u9054\u6210\u9019\u4e9b\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u8a73\u7d30\u7684\u5be6\u4f5c\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h3>\u6b65\u9a5f 1\uff1a\u8a2d\u5b9a UIImagePickerController<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 <strong>ViewController<\/strong> \u4e2d\u52a0\u5165 <strong>UIImagePickerController<\/strong>\uff0c\u4e26\u8a2d\u5b9a delegate\uff0c\u4ee5\u4fbf\u63a5\u6536\u4f7f\u7528\u8005\u9078\u64c7\u7684\u7167\u7247\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">import UIKit\n\nclass ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {\n    let imagePicker = UIImagePickerController()\n    \n    override func viewDidLoad() {\n        super.viewDidLoad()\n        imagePicker.delegate = self\n        imagePicker.sourceType = .photoLibrary\n    }\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 2\uff1a\u986f\u793a\u7167\u7247\u9078\u64c7\u5668<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>UIViewController<\/strong> \u7684\u65b9\u6cd5\u4f86\u986f\u793a\u7167\u7247\u9078\u64c7\u5668\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">@IBAction func selectPhoto(_ sender: UIButton) {\n    present(imagePicker, animated: true, completion: nil)\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 3\uff1a\u9078\u64c7\u7167\u7247\u5f8c\u88c1\u5207<\/h3>\n<p>\u7576\u4f7f\u7528\u8005\u9078\u64c7\u7167\u7247\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <strong>UIImagePickerController<\/strong> \u7684 delegate \u65b9\u6cd5\u4f86\u88c1\u5207\u7167\u7247\u3002\u5728\u9019\u88e1\uff0c\u6211\u5011\u5c07\u4f7f\u7528 <strong>CropViewController<\/strong> \u4f86\u9032\u884c\u88c1\u5207\u3002<\/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        let cropController = CropViewController(croppingStyle: .circular, image: image)\n        cropController.delegate = self\n        picker.pushViewController(cropController, animated: true)\n    }\n}\n<\/code><\/pre>\n<h3>\u6b65\u9a5f 4\uff1a\u7372\u53d6\u88c1\u5207\u5f8c\u7684\u7167\u7247<\/h3>\n<p>\u88c1\u5207\u5b8c\u6210\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u4f5c <strong>CropViewControllerDelegate<\/strong> \u4f86\u7372\u53d6\u88c1\u5207\u5f8c\u7684\u7167\u7247\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {\n    \/\/ \u5728\u9019\u88e1\u8655\u7406\u88c1\u5207\u5f8c\u7684\u7167\u7247\n    \/\/ \u4f8b\u5982\uff0c\u5c07\u88c1\u5207\u5f8c\u7684\u7167\u7247\u986f\u793a\u5728 UIImageView \u4e2d\n    imageView.image = image\n    cropViewController.dismiss(animated: true, completion: nil)\n}\n<\/code><\/pre>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u7576\u4f7f\u7528\u8005\u9078\u64c7\u7167\u7247\u6642\uff0c\u53ef\u80fd\u6703\u767c\u751f\u4e00\u4e9b\u932f\u8aa4\uff0c\u4f8b\u5982\u672a\u6388\u6b0a\u8a2a\u554f\u76f8\u7c3f\u6216\u9078\u64c7\u5668\u672a\u6b63\u78ba\u986f\u793a\u3002\u8acb\u78ba\u4fdd\u5728 <strong>Info.plist<\/strong> \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u9375\uff0c\u4ee5\u8acb\u6c42\u76f8\u7c3f\u8a2a\u554f\u6b0a\u9650\uff1a<\/p>\n<p>&#8220;`xml<br \/>\n<key>NSPhotoLibraryUsageDescription<\/key><br \/>\n<string>\u6211\u5011\u9700\u8981\u8a2a\u554f\u60a8\u7684\u76f8\u7c3f\u4ee5\u9078\u64c7\u7167\u7247\u3002<\/string><br \/>\n&#8220;`<\/p>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u7167\u7247\u9078\u64c7\u8207\u88c1\u5207\u529f\u80fd\uff0c\u60a8\u9084\u53ef\u4ee5\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u4f8b\u5982\u52a0\u5165\u5716\u7247\u6ffe\u93e1\u3001\u8abf\u6574\u4eae\u5ea6\u6216\u5c0d\u6bd4\u5ea6\u7b49\u3002\u5229\u7528 <strong>Core Image<\/strong> \u6846\u67b6\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u9019\u4e9b\u529f\u80fd\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u9078\u64c7\u7167\u7247\u5f8c\u9032\u884c\u88c1\u5207-\ud83d\udcf7_1675400836.131518.webp\" alt=\"Swift \u9078\u64c7\u7167\u7247\u5f8c\u9032\u884c\u88c1\u5207 \ud83d\udcf7\" title=\"Swift \u9078\u64c7\u7167\u7247, Swift \u88c1\u5207\u7167\u7247, Swift \u7167\u7247\u88c1\u5207, Swift \u7167\u7247\u9078\u64c7, Swift \u7167\u7247\u88c1\u526a\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54<\/h2>\n<h3>Q1: \u5982\u4f55\u7372\u53d6\u4f7f\u7528\u8005\u62d2\u7d55\u76f8\u7c3f\u8a2a\u554f\u7684\u60c5\u6cc1\uff1f<\/h3>\n<p>A1: \u60a8\u53ef\u4ee5\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u6aa2\u67e5\u76f8\u7c3f\u8a2a\u554f\u72c0\u614b\uff0c\u4e26\u6839\u64da\u72c0\u614b\u63d0\u4f9b\u76f8\u61c9\u7684\u63d0\u793a\u6216\u884c\u52d5\u3002<\/p>\n<h3>Q2: \u4f7f\u7528 <strong>CropViewController<\/strong> \u6642\uff0c\u6709\u6c92\u6709\u9700\u8981\u6ce8\u610f\u7684\u9ede\uff1f<\/h3>\n<p>A2: \u78ba\u4fdd\u60a8\u5df2\u6b63\u78ba\u8a2d\u7f6e <strong>delegate<\/strong>\uff0c\u4e26\u5728\u88c1\u5207\u64cd\u4f5c\u5b8c\u6210\u5f8c\u9069\u7576\u5730\u8655\u7406\u88c1\u5207\u5f8c\u7684\u5716\u7247\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5728\u88c1\u5207\u5f8c\u7684\u5716\u7247\u4e0a\u6dfb\u52a0\u6ffe\u93e1\u6548\u679c\uff1f<\/h3>\n<p>A3: \u60a8\u53ef\u4ee5\u4f7f\u7528 <strong>Core Image<\/strong> \u6846\u67b6\u4f86\u5275\u5efa\u548c\u61c9\u7528\u6ffe\u93e1\uff0c\u9019\u6a23\u53ef\u4ee5\u5be6\u73fe\u66f4\u8c50\u5bcc\u7684\u5716\u7247\u8655\u7406\u6548\u679c\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u8a9e\u8a00\u4f86\u9078\u64c7\u7167\u7247\u5f8c\u9032\u884c\u88c1\u5207\uff0c\u4e26\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u7684methods()\u51fd\u6578\u4f86\u5b8c\u6210\u88c1\u5207\u7684\u52d5\u4f5c\u3002<\/p>\n","protected":false},"author":1,"featured_media":7021,"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-7022","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-\u9078\u64c7\u7167\u7247\u5f8c\u9032\u884c\u88c1\u5207-\ud83d\udcf7_1675400836.131518.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Pg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7022","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=7022"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7022\/revisions"}],"predecessor-version":[{"id":13292,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7022\/revisions\/13292"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7021"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}