简体   繁体   English

iOS Swift base64encoding与PAW应用程序Base64 Encoding不同

[英]iOS Swift base64encoding different than PAW app Base64 Encoding

Hoping to get some help with this. 希望对此有所帮助。 I am trying to post an image to a server and it requires base64 encoding of a PNG file. 我正在尝试将图像发布到服务器,并且它需要PNG文件的base64编码。 When I use the PAW app and encode the image everything shows up on the server beautifully. 当我使用PAW应用程序并对图像进行编码时,一切都会很好地显示在服务器上。 When I attempt to do the same with iOS Swift 4, the string produced is similar but also has differences and thus an incorrect image. 当我尝试对iOS Swift 4进行相同操作时,生成的字符串是相似的,但也有所不同,因此图像不正确。 Any idea of how to match the string that is correctly created in the PAW app in iOS. 关于如何匹配在iOS的PAW应用中正确创建的字符串的任何想法。 I have included code below along with screenshots of the strings (small samples) created. 我在下面包括了代码以及创建的字符串(小样本)的屏幕截图。

Thanks! 谢谢!

    let image : UIImage = UIImage(named:"STG.png")!
    let imageData = UIImagePNGRepresentation(image)
    var base64String = imageData?.base64EncodedString(options: [])

爪子图像

在此处输入图片说明

You are not comparing the same data at all. 您根本没有比较相同的数据。 Loading the png into a UIImage and then converting the UIImage into a new png representation does not result in the same set of bytes at all. 将png加载到UIImage ,然后将UIImage转换为新的png表示形式根本不会导致相同的字节集。

You need to directly load the png file into a Data instance without doing any conversion. 您需要直接将png文件加载到Data实例中,而无需进行任何转换。

let imageURL = Bundle.main.url(forResource: "STG", withExtension: "png")!
let imageData = try! Data(contentsOf: imageURL)
var base64String = imageData.base64EncodedString(options: [])

You might also need to try different options in the call to base64EncodedString . 您可能还需要在对base64EncodedString的调用中尝试其他选项。

Ok went down several rabbit holes and found some folks with similar issues to this. 好了,钻了几个兔子洞,发现了一些与此相似的人。 I eventually solved it by switching over to Alamofire instead of using the native urlsession. 我最终通过切换到Alamofire而不是使用本机urlsession解决了它。 I also found out the server I was posting to would allow for multipart/form-data as well which I ended up using for my request. 我还发现我要发布到的服务器也将允许多部分/表单数据,最终我将其用于我的请求。 I was unable to get that to work using the native urlsession either. 我也无法使用本地urlsession使它正常工作。

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

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