ios - 拍照不保存到手机

标签 ios swift

我正在开发一个 ios 应用程序,用户可以在其中使用手机摄像头拍摄图像并通过电子邮件发送,而无需将其保存在手机中(出于保密原因)。
我在谷歌中搜索但找不到任何有用的东西。
这可能吗 ?如果没有,是否有任何变通办法?

最佳答案

试试这个,我只是为你制作的,试试临时文件的部分,我在最后告诉我是谁做的。问候

import UIKit
import MobileCoreServices
import MessageUI

class PictureViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }


    @IBAction func editImage()
    {
        let alerta = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        var accionCamera = UIAlertAction()
        if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
        {
            accionCamera = UIAlertAction(title: "Take a picture", style: .default, handler: { (ACTION) in
                self.pickMediaFromSource(UIImagePickerControllerSourceType.camera)
            })
            alerta.addAction(accionCamera)
        }

        let accionNo = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alerta.addAction(accionNo)
        present(alerta, animated: true, completion: nil)
    }

    func pickMediaFromSource(_ sourceType:UIImagePickerControllerSourceType)
    {
        let mediaTypes = UIImagePickerController.availableMediaTypes(for: sourceType)!
        if UIImagePickerController.isSourceTypeAvailable(sourceType) && mediaTypes.count > 0
        {
            let picker = UIImagePickerController()
            picker.mediaTypes = mediaTypes
            picker.delegate = self
            picker.sourceType = sourceType

            present(picker, animated: true, completion: nil)
        }
        else
        {
            let alertController = UIAlertController(title:"Error", message: "Media not supported", preferredStyle: UIAlertControllerStyle.alert)
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.destructive, handler: nil)
            alertController.addAction(okAction)
            present(alertController, animated: true, completion: nil)
        }
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {
        let lastChosenMediaType = info[UIImagePickerControllerMediaType] as? String
        if let mediaType = lastChosenMediaType
        {
            if mediaType == kUTTypeImage as String
            {
                let image = info[UIImagePickerControllerOriginalImage] as? UIImage
                picker.dismiss(animated: true) {
                    self.sendEmailFront(image: image!)
                }

                let tmpDirectory = try? FileManager.default.contentsOfDirectory(atPath: NSTemporaryDirectory())

            }
            else if mediaType == kUTTypeMovie  as String
            {
                picker.dismiss(animated: true, completion: {
                    let alertController = UIAlertController(title:"Error", message: "Medio no soportado. Debe ser una imagen", preferredStyle: UIAlertControllerStyle.alert)
                    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.destructive, handler: nil)
                    alertController.addAction(okAction)
                    self.present(alertController, animated: true, completion: nil)

                })
                return

            }
        }
    }


    func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
    {
        picker.dismiss(animated: true, completion:nil)
    }

    // MARK: - SEND EMAIL
    func sendEmailFront(image: UIImage)
    {
        if MFMailComposeViewController.canSendMail()
        {
            let dataImage = UIImagePNGRepresentation(image)
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients(["example@gmail.com"])
            mail.addAttachmentData(dataImage!, mimeType: "THE MIME TYPE", fileName: "THE FILE NAME")

            self.present(mail, animated: true, completion: nil)
        }
    }
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true) {

    // TO CHECK IF EXIST ANY FILE TO DELETE, I DON'T FIND ANYTHING BUT, TRY YOU ANYWAY
            let tmpDirectory = try? FileManager.default.contentsOfDirectory(atPath: NSTemporaryDirectory())

            let urls = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first
            let contents = try? FileManager.default.contentsOfDirectory(at: urls!, includingPropertiesForKeys: nil, options: .skipsPackageDescendants)
            print(contents)

        }
    }


}

关于ios - 拍照不保存到手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52017997/

相关文章:

ios - 将 id<MTLTexture> 从 ios 发送到 unity

objective-c - 动画删除注释

ios - 尝试从完成处理程序中的响应对象返回字符串

ios - iMessage 应用程序扩展可以使用主机应用程序中定义的 View Controller 吗?

iphone - (#゚Д゚) 是一个 5 个字母的单词。但是在 iOS 中,[@"(# ゚Д゚)"length] 是 7。为什么?

ios - 无法使用类型为 '(UInt64)' 的参数列表调用

swift - 在两个 View Controller Swift 之间传递变量时出错

ios - 动画 CAShapeLayer 路径变化

ios - 从 xib addTarget 加载的按钮不执行操作

ios - 按下后工具栏消失