[iOS9][Xcode7]图片base64上传到服务器后,服务器上的图片损坏问题



背景:正在做一个项目,商品维护功能中需要将商品拍照,图片上传到服务器。

问题描述:RT;拍照获得的图片在手机上显示正常,但服务器上的图片文件有大小,可无法打开,显示损坏。

解决过程:

1、一开始怀疑是框架中的拍照sdk返回的NSData有误,然后用UIImage对象自己生成NSData上传,仍然有问题;

2、怀疑base64规则和服务端的不一致;图片数据base64编码是使用官方sdk中的base64EncodedStringWithOptions:方法,但此方法提供了四个不同的选项,如下:

/****************        Base 64 Options	****************/

typedef NS_OPTIONS(NSUInteger, NSDataBase64EncodingOptions) {
    // Use zero or one of the following to control the maximum line length after which a line ending is inserted. No line endings are inserted by default.
    NSDataBase64Encoding64CharacterLineLength = 1UL << 0,
    NSDataBase64Encoding76CharacterLineLength = 1UL << 1,

    // Use zero or more of the following to specify which kind of line ending is inserted. The default line ending is CR LF.
    NSDataBase64EncodingEndLineWithCarriageReturn = 1UL << 4,
    NSDataBase64EncodingEndLineWithLineFeed = 1UL << 5,

} NS_ENUM_AVAILABLE(10_9, 7_0);

3、挨个尝试了以上四个选项之后,发现选项中必须带有NSDataBase64EncodingEndLineWithCarriageReturn或NSDataBase64EncodingEndLineWithLineFeed,上传后的图片才能够正常显示。

猜你喜欢

转载自blog.csdn.net/jhq1990/article/details/49679593