从UIImagePickerController获取图像是iOS开发中常见的任务之一。UIImagePickerController是一个提供照片和视频选择器的UI组件。以下是使用UIImagePickerController获取图像的完整步骤:
import UIKit
import Photos
<key>NSPhotoLibraryUsageDescription</key><string>App需要您的同意才能访问您的照片库</string>
func selectImage() {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = .photoLibrary
imagePickerController.allowsEditing = false
present(imagePickerController, animated: true, completion: nil)
}
class YourViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// Your view controller code
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
if let selectedImage = info[.originalImage] as? UIImage {
// Use the selectedImage as you wish
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
selectImage()
函数以呈现UIImagePickerController:selectImage()
通过这些步骤,您可以使用UIImagePickerController从照片库中获取图像。请注意,这个答案没有提及其他云计算品牌商,因为它是关于从UIImagePickerController获取图像的问题。
领取专属 10元无门槛券
手把手带您无忧上云