拍照而不保存到手机

时间:2018-08-25 13:57:41

标签: ios swift

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

1 个答案:

答案 0 :(得分:0)

尝试一下,我只是为您准备的,尝试使用temp文件的一部分,最后我知道是谁做的。问候

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)

        }
    }


}