繁体   English   中英

在Swift 2.3中将UIImage转换为Base64

[英]Convert UIImage to Base64 in swift 2.3

我正在尝试使用swift 2.3将UIImage转换为Base64编码字符串,但是编码后的字符串无法发布到服务器上。

服务器抛出诸如“处理异常时抛出异常(ErrorException:iconv():在输入字符串中检测到非法字符)之类的错误”。

我可以从编码的字符串中删除特殊字符还是源代码中的某些问题,因为我在下面提到了我的源代码。

 //*************** 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 

文档

选项
指定用于Base-64编码数据的选项的掩码。 可能的值在NSDataBase64EncodingOptions中给出。

因此,您需要将NSDataBase64EncodingOptions()替换为NSDataBase64EncodingOptions().[one_of_the_following_values]

NSDataBase64Encoding64CharacterLineLength
将最大行长度设置为64个字符,然后插入行尾。

NSDataBase64Encoding76CharacterLineLength
将最大行长度设置为76个字符,然后插入行尾。

NSDataBase64EncodingEndLineWithCarriageReturn
设置最大行长后,请指定要插入的行尾应包含回车符。

NSDataBase64EncodingEndLineWithLineFeed
设置最大行长后,请指定要插入的行尾应包含换行符。

资料来源: Apple文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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