Swift无效的Base64 - 字符串连接

时间:2015-03-27 01:35:48

标签: ios swift

所有

这是一个两部分问题......

首先,我试图基于64编码图像:

    var finalMessage = "";
    if(imageBytes != nil && imageBytes.length > 0){
        finalMessage += " <image>";
        finalMessage += imageBytes!.base64EncodedStringWithOptions(nil);
        finalMessage += "</image>";
    }

当我在最后一个连接setter之后调试并检查finalMessage时,我将此作为调试器中的值:

"\U0000fffc <image>/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAF3KADAAQAAAABAAAD6QAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgD6QXcAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/bAEMAFhYWFhYWJhYWJjYmJiY2STY2NjZJXElJSUlJXG9cXFxcXFxvb29vb29vb4aGhoaGhpycnJycr6+vr6+vr6+vr//bAEMBGx0dLSktTCkpTLd8Zny3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t//dAAQ"

它很长,所以向右滚动以查看上面代码的第6行中没有</image>标记。很奇怪....

那么,base 64编码是否有大小限制?知道这里可能会发生什么吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

除了可用之外,Base64编码的字符串没有大小限制 记忆。特别是

中的第二个陈述没有理由
    finalMessage += imageBytes!.base64EncodedStringWithOptions(nil);
    finalMessage += "</image>";

什么都不做。但调试器可能会限制输出(可能是 64K?),如有疑问,请查看NSLog()println()

答案 1 :(得分:0)

var finalString = ""
let screenshot = UIImage(named: "screen")
let screenshotData = UIImagePNGRepresentation(screenshot)
let screenshotBase64 = screenshotData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) as NSString

if (screenshotBase64 != "") {
    let tempString = screenshotBase64.stringByAppendingString("</image>")
    finalString = "<image>\(tempString)"
}

println(finalString)

替换&#34;屏幕&#34;无论您的图像名称是什么,请检查它是否有效。