简体   繁体   中英

Convert UIImage to Base64 in swift 2.3

I am trying to convert UIImage to Base64 encoding string using swift 2.3,But encoded string unable to post on the server.

Server throws the error like "Exception thrown when handling an exception (ErrorException: iconv(): Detected an illegal character in input string)".

Can I remove special characters from the encoded string or some issue with my source code.as i have mention my source code below.

 //*************** Image Picker function ********* //
 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
 {
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    self.imageData = UIImageJPEGRepresentation(chosenImage, 0)         
    self.imageString = self.imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
   self.webServiceForShareData()
   dismissViewControllerAnimated(true, completion: nil)
 }//******** Image Picker End **** //



 func webServiceForShareData()
{
 let allowedCharacters = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as! NSMutableCharacterSet
    allowedCharacters.removeCharactersInString("+/=")


     //****************** Alamofire Request *********** // 
    Alamofire.request(.POST,"URL",parameters:["pic":self.imageString != nil ? (self.imageString!.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacters))! :"", "txt" : self.txtTest.text!, "on_twitter" : self.twitterSuccess, "on_facebook" : self.facebookSuccess],headers : ["Authorization":(NSUserDefaults.standardUserDefaults().objectForKey("oneSocialToken") as! String)])
        .responseJSON
        { 
             response in
             print(response.request)  // original URL request
             print(response.response) // URL response
             print(response.data)     // server data
             print(response.result)   // result of response serializatio
            if let JSON: NSDictionary = response.result.value as? NSDictionary
            {
                print(JSON)
            }
       }
 //*************** Alamofire Request End *************** //

 }// *** Function End 

From the docs :

options
A mask that specifies options for Base-64 encoding the data. Possible values are given in NSDataBase64EncodingOptions.

So you need to replace your NSDataBase64EncodingOptions() with NSDataBase64EncodingOptions().[one_of_the_following_values] :

NSDataBase64Encoding64CharacterLineLength
Set the maximum line length to 64 characters, after which a line ending is inserted.

NSDataBase64Encoding76CharacterLineLength
Set the maximum line length to 76 characters, after which a line ending is inserted.

NSDataBase64EncodingEndLineWithCarriageReturn
When a maximum line length is set, specify that the line ending to insert should include a carriage return.

NSDataBase64EncodingEndLineWithLineFeed
When a maximum line length is set, specify that the line ending to insert should include a line feed.

Source: Apple docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM