我有一个项目,要求我拍照并将其发送到提供的API。我不想将其保存在本地,一旦捕获到图片,就应立即将其发送到API。
我已经检查了堆栈溢出问题swift Take a photo and save to photo library,但似乎图像存储在本地。
这是我的代码:
func cameraSetup(){
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)
do {
let input = try AVCaptureDeviceInput(device: captureDevice!)
captureSession = AVCaptureSession()
captureSession?.addInput(input)
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
videoPreviewLayer?.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height - 150)
self.cameraView.layer.addSublayer(videoPreviewLayer!)
captureSession?.startRunning()
} catch {
print(error)
}
}
}
我如何立即发送它,而不必先将其保存在本地?
答案 0 :(得分:0)
对于这样的任务,您应该考虑使用UIImagePickerController
及其Delegate方法。最佳做法是显示用户选项,以在相机和图库(照片库)之间进行选择。
class MyViewController: UIViewController {
func presentImagePickerActionSheet() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
func addCameraAction() {
guard UIImagePickerController.isSourceTypeAvailable(.camera) else { return assertionFailure("No camera") }
let pickerAction = UIAlertAction(title: "Camera", style: .default) { _ in
self.pickImage(source: .camera)
}
actionSheet.addAction(pickerAction)
}
func addGalleryPickerAction() {
guard UIImagePickerController.isSourceTypeAvailable(.photoLibrary) else { return assertionFailure("No gallery") }
let pickerAction = UIAlertAction(title: "Gallery", style: .default) { _ in
self.pickImage(source: .photoLibrary)
}
actionSheet.addAction(pickerAction)
}
func addRemoveActionIfNeeded() {
return() // Do your logic if needed
let pickerAction = UIAlertAction(title: "Delete", style: .destructive) { _ in
}
actionSheet.addAction(pickerAction)
}
func addCancelAction() {
let cancelAction = UIAlertAction(title: "cancel", style: .cancel, handler: nil)
actionSheet.addAction(cancelAction)
}
addCameraAction()
addGalleryPickerAction()
addRemoveActionIfNeeded()
addCancelAction()
present(actionSheet, animated: true)
}
private func pickImage(source: UIImagePickerControllerSourceType) {
guard UIImagePickerController.isSourceTypeAvailable(source) else { return assertionFailure("Source not found") }
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = source
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
present(imagePickerController, animated: true)
}
}
extension MyViewController: UIImagePickerControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
defer { picker.dismiss(animated: true) }
let editedImage = info[UIImagePickerControllerEditedImage]
let originalImage = info[UIImagePickerControllerOriginalImage]
guard let image = (editedImage ?? originalImage) as? UIImage else { return assertionFailure("Image not found")}
// Do anything you want with image here
// In case of need to convert it to data:
let quality = 1.0
guard let imageData = UIImageJPEGRepresentation(image, quality) else { return assertionFailure("No image data") }
// Do anything you want with image data here
}
}