如何将字符串转换为UIImage?

时间:2016-11-15 07:47:49

标签: swift alamofire

我有字典,我把一些值

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"]!
        ]

并且在值upload2 / upload3中我保存了base64String数组。

我需要将upload2 / upload3数组转换为UIImageJPEGRepresentation,并使用Alamofire将其转换为多部分表单数据。我的代码是:

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
            }
        })

但是我有一个错误:  enter image description here

错误是:

2016-11-15 13:38:18.027 kp[6504:146465] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (UIImage)'

1 个答案:

答案 0 :(得分:1)

转换时可能出错

Swift3:正确格式化base64String并尝试

数据到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)