在 swift 2.3 中将 UIImage 转换为 Base64
Convert UIImage to Base64 in swift 2.3
我正在尝试使用 swift 2.3 将 UIImage 转换为 Base64 编码字符串,但编码字符串无法在服务器上 post。
服务器抛出类似 "Exception thrown when handling an exception (ErrorException: iconv(): Detected an illegal character in input string)" 的错误。
我可以从编码字符串中删除特殊字符吗?或者我的源代码有问题code.as我在下面提到了我的源代码。
//*************** 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
来自docs:
options
A mask that specifies options for Base-64 encoding the data. Possible values are given in NSDataBase64EncodingOptions.
因此您需要将 NSDataBase64EncodingOptions()
替换为 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.
来源:Apple docs
我正在尝试使用 swift 2.3 将 UIImage 转换为 Base64 编码字符串,但编码字符串无法在服务器上 post。
服务器抛出类似 "Exception thrown when handling an exception (ErrorException: iconv(): Detected an illegal character in input string)" 的错误。
我可以从编码字符串中删除特殊字符吗?或者我的源代码有问题code.as我在下面提到了我的源代码。
//*************** 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
来自docs:
options
A mask that specifies options for Base-64 encoding the data. Possible values are given in NSDataBase64EncodingOptions.
因此您需要将 NSDataBase64EncodingOptions()
替换为 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.
来源:Apple docs