-1

i'm writing web api for an application for android and IOS, and i'm having an issue convert base64 to image only for IOS, for android they are giving correct string in base64 format. I can able to convert it as image. but in IOS they are give me a string starting like this /9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAGhKADAAQAAAABAAAJxAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs. In this string contain some empty spaces and i had removed it like this,

string images = model.RequestObject.UserImage.ToCharArray().Where(c=>!Char.IsWhiteSpace(c)).Select(c => c.ToString()).Aggregate((a, b) => a + b);

While convert string as image, an error message thrown like string is not base64 format. and this is how i'm converting it

File.WriteAllBytes(StoragePath, Convert.FromBase64String(model.RequestObject.UserImage));
                model.RequestObject.UserImage = FileName;

i had tried like this

var bytes = Convert.FromBase64String(resizeImage.Content); using (var imageFile = new FileStream(filePath, FileMode.Create)) { imageFile.Write(bytes ,0, bytes.Length); imageFile.Flush(); }

But my api says it not valid string. Can somebody help me how to fix this issue. thanks!

wpclevel
  • 2,451
  • 3
  • 14
  • 33

1 Answers1

1

Try to encode like this way

let utf8str = yourString.dataUsingEncoding(NSUTF8StringEncoding)
let base64Encoded = utf8str.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)

Hope this will help.

Nirav D
  • 71,513
  • 12
  • 161
  • 183