UIImageWriteToSavedPhotosAlbum将照片从相机保存到后期

时间:2018-10-24 09:20:34

标签: ios swift

如何获取从相机拍摄的照片的网址?关闭对话框后保存照片。 权限:

  • 隐私-图片库使用说明

  • 隐私-图片库添加用法说明

  • 隐私-相机使用说明

图像控制器

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
    UIImageWriteToSavedPhotosAlbum(pickedImage!, self, nil, nil)

    guard let document = getDocument(from: info) else {
        picker.dismiss(animated: true, completion: nil)
        return
    }
    viewModel?.documens.append(document)
    documentsCollectionView.reloadData()
    picker.dismiss(animated: true, completion: nil)
}

private func getDocument(from imageInfo: [String: Any]) -> DocumentRepresentable? {
    guard let selectedImage = imageInfo[UIImagePickerControllerOriginalImage] as? UIImage,
        let imageURL = imageInfo[UIImagePickerControllerReferenceURL] as? URL else {
            return nil
    }
    return Document(path: imageURL.path, data: selectedImage)
}

打开相机功能

  private func showCameraPicker() {
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            let myPickerController = UIImagePickerController()
            myPickerController.sourceType = .camera
            myPickerController.delegate = self

            viewModel?.context?.present(myPickerController, animated: true, completion: nil)
        }
    }

0 个答案:

没有答案