如何在 Swift 中从相机或照片库加载图像 [关闭]
Posted
技术标签:
【中文标题】如何在 Swift 中从相机或照片库加载图像 [关闭]【英文标题】:How to load image from camera or photo library in Swift [closed] 【发布时间】:2016-10-01 23:20:17 【问题描述】:例如,您如何使用相机拍照或从照片库中选择图像以便能够将其显示在 imageView 中?
对此的任何帮助将不胜感激。
【问题讨论】:
我已经为你发布了一个答案,但是你得到了所有的反对,因为这不是“为你编码”的网站。您必须提供代码,我们正在帮助您查找错误并改进您的代码。所以下一次,如果你遇到困难,请尝试谷歌解决方案并获得帮助。现在:我已经为您发布了一个功能齐全的选项。玩得开心 我知道这不是人们为我工作的地方,我只是想知道我可以从哪里开始。我不希望任何人为我写出来,尽管我很欣赏你发布的答案。 我不想冒犯你或任何朋友。只是想告诉你,为什么你的票数都被否决了。你得到了我的支持,所以它不会变得太糟糕,如果我的工作对你有帮助,我很高兴回答你的问题,以便你检查它。如果您需要进一步的帮助,请告诉我。 【参考方案1】:Swift 5+ 的完整更新,包括 Github Repo
首先,我们要将所有内容组织到一个专门的类中。
import MobileCoreServices
import UIKit
class CameraProvider: NSObject
enum PhotoLibraryTypes
case photoLibrary, savedPhotosAlbum
var casted: UIImagePickerController.SourceType
switch self
case .photoLibrary: return UIImagePickerController.SourceType.photoLibrary
case .savedPhotosAlbum: return UIImagePickerController.SourceType.savedPhotosAlbum
public typealias SourceType = UIImagePickerController.SourceType
public typealias ImagePicker = UIImagePickerController
public typealias Delegate = UINavigationControllerDelegate & UIImagePickerControllerDelegate
private let delegate: Delegate
init(delegate: Delegate)
self.delegate = delegate
// MARK: - Public
public func getImagePicker(source: PhotoLibraryTypes,
canEditPhotos: Bool = true,
onlyImages: Bool = false) throws -> ImagePicker
do
return try getBaseController(
source: source.casted,
allowsEditing: canEditPhotos,
onlyImages: onlyImages
)
catch
throw error
public func getCamera(canEditPhotos: Bool = true,
onlyImages: Bool = false) throws -> ImagePicker
do
let picker = try getBaseController(
source: .camera,
allowsEditing: canEditPhotos,
onlyImages: onlyImages
)
if UIImagePickerController.isCameraDeviceAvailable(.rear)
picker.cameraDevice = .rear
else if UIImagePickerController.isCameraDeviceAvailable(.front)
picker.cameraDevice = .front
else
throw "No known camera type available"
picker.showsCameraControls = true
return picker
catch
throw error
// MARK: - Private
private func getBaseController(source: SourceType,
allowsEditing: Bool,
onlyImages: Bool) throws -> ImagePicker
guard UIImagePickerController.isSourceTypeAvailable(source) else
throw "Requested source not available"
let picker = UIImagePickerController()
let imageType = kUTTypeImage as String
picker.sourceType = source
picker.allowsEditing = allowsEditing
picker.delegate = self.delegate
if onlyImages,
let mediaTypes = UIImagePickerController.availableMediaTypes(for: source),
mediaTypes.contains(imageType)
picker.mediaTypes = [imageType]
return picker
extension String: LocalizedError
public var errorDescription: String? return self
最后我们调用我们的CameraProvider
如下。
import UIKit
class ViewController: UIViewController
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(animated)
let provider = CameraProvider(delegate: self)
do
let picker = try provider.getImagePicker(source: .photoLibrary)
present(picker, animated: true)
catch
NSLog("Error: \(error.localizedDescription)")
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
let image = (
info[UIImagePickerController.InfoKey.editedImage] as? UIImage ??
info[UIImagePickerController.InfoKey.originalImage] as? UIImage
)
picker.dismiss(animated: true, completion: nil)
【讨论】:
很好的回答大卫谢谢分享 好吧,不客气 您的代码运行良好,但使用 canEdit 从库加载时除外:当我选择图像时它会崩溃 非常有用的答案。谢谢你。但我猜有一个小错误。您在 getPhotoLibraryOn 中检查了两次 isPhotoLibraryAvailable。第二个应该是 isSavedPhotoAlbumAvailable。 太棒了!完美运行。谢谢你以上是关于如何在 Swift 中从相机或照片库加载图像 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
iPhone:如何使用 iPhone 照片应用程序等“编辑”功能从照片库或相机中获取图像