通过BASE64或文件上的POST请求将UIImageview上创建的图像发送到服务器

时间:2019-07-05 00:14:18

标签: post uiimageview uiimage alamofire alamofireimage

我正在尝试发送由用户使用自己的手指捕获的图像,该图像是由Swift 4.0在UIImage上绘制的,“如何像文件一样发送?”或“如何获取和转换”输入BASE64 String ?,服务器准备在BASE64上接收图像,而在File上接收则很容易,关键是如何在iOS上通过用户设备发送?

I try the Follow code, first using the simple example of Alamofire github, the second it's some I adapt

//The imageView's
   @IBOutlet weak var mainImageView: UIImageView!
    @IBOutlet weak var tempImageView: UIImageView!
//properties to drawing:
    var lastPoint = CGPoint.zero
    var color = UIColor.blue
    var brushWidth: CGFloat = 10.0
    var opacity: CGFloat = 1.0
    var swiped = false

//functions to get draw by fingers:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return
        }
        swiped = false
        lastPoint = touch.location(in: view)
    }


    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return
        }

        // 6
        swiped = true
        let currentPoint = touch.location(in: view)
        drawLine(from: lastPoint, to: currentPoint)

        // 7
        lastPoint = currentPoint
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !swiped {
            // draw a single point
            drawLine(from: lastPoint, to: lastPoint)
        }

        // Merge tempImageView into mainImageView
        UIGraphicsBeginImageContext(mainImageView.frame.size)
        mainImageView.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1.0)
        tempImageView?.image?.draw(in: view.bounds, blendMode: .normal, alpha: opacity)
        mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        tempImageView.image = nil
    }


    func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) {
        // 1
        UIGraphicsBeginImageContext(view.frame.size)
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        tempImageView.image?.draw(in: view.bounds)

        // 2
        context.move(to: fromPoint)
        context.addLine(to: toPoint)

        // 3
        context.setLineCap(.round)
        context.setBlendMode(.normal)
        context.setLineWidth(brushWidth)
        context.setStrokeColor(color.cgColor)

        // 4
        context.strokePath()

        // 5
        tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
        tempImageView.alpha = opacity
        UIGraphicsEndImageContext()
    }

//First method
    func uploadImageAndData(){
        let url = "https://www.myurl/datarecept.php"

        let imageData = mainImageView!.image!.pngData()!
        Alamofire.upload(imageData, to: url).responseJSON { response in
            debugPrint(response)
        }
    }

//Second method

    func uploadMultiformData2(){

        let imageData = (mainImageView.image ?? tempImageView as! UIImage).pngData()!

        Alamofire.upload(imageData, to: url).responseJSON { response in
            debugPrint(response)
        }
    }

Nothing Happen's Wat's Wrong

0 个答案:

没有答案