Я пытаюсь отправить изображение было захвачено пользователем, используя его собственными пальцами, это изображение рисуется на UIImage с помощью Swift 4.0, ¿как я могу отправить, как файл?, Или or как можно получить и преобразоватьв строку BASE64 ?, сервер готов получить изображение на BASE64, но легко получить файл, вопрос в том, как отправить на устройство пользователя на 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