首页 > 解决方案 > 拍照不保存到手机

问题描述

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

标签: iosswift

解决方案


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

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)

        }
    }


}

推荐阅读