0

I have dictionary whre i put some values

let params:[String:Any?]=[
            "upload":(postInfo["upload"]?.isEmpty)! ? nil : UIImage(data: Data.init(base64Encoded: postInfo["upload"]!)!),
            "type-place":postInfo["type-place"]!,
            "place":postInfo["place"]!,
            "city-place":postInfo["city-place"]!,
            "name-place":postInfo["name-place"]!,
            "web-site":postInfo["web-site"]!,
            "e-mail":postInfo["e-mail"]!,
            "mob-tel":postInfo["mob-tel"]!,
            "mob-tel-dop":postInfo["mob-tel-dop"]!,
            "adress":postInfo["adress"]!,
            "adressMapH":postInfo["adressMapH"]!,
            "content":postInfo["content"]!,
            "work-day":postInfo["work-day"]!,
            "priceS":postInfo["priceS"]!,
            "priceE":postInfo["priceE"]!,
            "countPlaceS":postInfo["countPlaceS"]!,
            "countPlaceE":postInfo["countPlaceE"]!,
            "link-vk":postInfo["link-vk"]!,
            "link-inst":postInfo["link-inst"]!,
            "link-fs":postInfo["link-fs"]!,
            "link-twitter":postInfo["link-twitter"]!,
            "upload1":(postInfo["upload1"]?.isEmpty)! ? nil : UIImage(data: Data.init(base64Encoded: postInfo["upload1"]!)!),
            "halal":(postInfo["properties"]?.contains("халяль"))! ? "1":"0",
            "wifi":(postInfo["properties"]?.contains("wi-fi"))! ? "1":"0",
            "card":(postInfo["services"]?.contains("оплата картой"))! ? "1":"0",
            "vip":(postInfo["services"]?.contains("гардероб"))! ? "1":"0",
            "parking":(postInfo["services"]?.contains("парковка"))! ? "1":"0",
            "garderob":(postInfo["services"]?.contains("гардероб"))! ? "1":"0",
            "upload2":postInfo["upload2"],
            "upload3":postInfo["upload3"],
            "isAkc":(postInfo["descActionSale"]?.isEmpty)! ? "0":"1",
            "descActionSale":postInfo["descActionSale"]!,
            "deposit":postInfo["deposit"]!
        ]

and in value upload2/upload3 i save base64String array.

I need to convert upload2/upload3 array to UIImageJPEGRepresentation and put it to multipart form data using Alamofire. There my code:

Alamofire.upload(multipartFormData: {
            multipartFormData in
            for (key,value) in params{
                if key == "upload2"{
                    for item in (params["upload2"] as! String).components(separatedBy: ","){

                        let decodedData = Data.init(base64Encoded: item, options: Data.Base64DecodingOptions.init(rawValue: 0))
                        if  let img=UIImage(data: decodedData!){
                            let m=UIImageJPEGRepresentation(img, 1)
                            multipartFormData.append(m!, withName: key, mimeType: "image/jpeg")
                        }
                    }
                }else if key == "upload3"{
                    for item in (params["upload3"] as! String).components(separatedBy: ","){
                        if  let img=UIImage(data: Data.init(base64Encoded: item)!){
                            let m=UIImageJPEGRepresentation(img, 1)
                            multipartFormData.append(m!, withName: key, mimeType: "image/jpeg")
                        }
                    }
                }else{
                    multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
                }
            }
        }, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: "http://kuda-poydem.kz/apitest.php?act=org_add&add-new-place=1", method: .post, headers: headers, encodingCompletion: {
            (encodingResult) in
            switch encodingResult{
            case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):
                upload.responseJSON{
                    response in
                    print(response)
                }
                break
            default:
                break
            }
        })

But there i got an error: enter image description here

Error is :

2016-11-15 13:38:18.027 kp[6504:146465] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (UIImage)'
beka.angsabay
  • 413
  • 1
  • 6
  • 15
  • 3
    You cannot serialize `UIImage` to JSON, that's what the error message says. To upload `Data` you need to encode it to a base64 encoded string. – vadian Nov 15 '16 at 07:53
  • Is your string a base64 encoded string? If yes, then converting it to UIImage should be [fairly simple](http://stackoverflow.com/questions/11251340/convert-between-uiimage-and-base64-string) Convert the string to data and convert NSData to UIImage – NSNoob Nov 15 '16 at 07:56
  • I am using UIImageJPEGRepresentation(selectedImage, 1)?.base64EncodedString(). Is it proper? – beka.angsabay Nov 15 '16 at 08:34

1 Answers1

3

May you gone wrong in conversion

Swift3: Format base64String Correctly and try

Data to Base64String

var base64String: NSString!
let myImage = UIImage(named:"image.png")
let imageData = UIImageJPEGRepresentation(myImage, 0.9)
base64String = imageData!.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithLineFeed) as NSString!
print(base64String)

Base64String to Data

let thumbnail1Data =  Data(base64Encoded: base64String as String, options: NSData.Base64DecodingOptions()
imageView?.image = UIImage(data: thumbnail1Data as Data)
Sanju
  • 1,148
  • 11
  • 26