美文网首页
IOS 解析过滤之后的base64码

IOS 解析过滤之后的base64码

作者: Albert新荣 | 来源:发表于2020-06-10 10:55 被阅读0次

    由于最近做人脸识别,后台上传到服务器的时候,发现补位码”=“出现的时候,人脸识别的注册图无法上传成功,经过测试发现,只要有补位码出现"="的时候就会无法上传成功,和图片的特征,人脸数据的矩阵方差没啥关系,
    这个记录一下,过滤一些会被后台误认为是空字符的特殊字符 ”\r“ ”\n“ ”="

    UIImage 转码到 NSData

    -(void)reuploadAction:(UIButton *)sender{
        [XNHUD show];
       //压缩到指定的KB 列如下面的200 直接 pod 'YQImageCompressor' 可以引入
        [YQImageCompressTool CompressToDataAtBackgroundWithImage:self.cameraBtn.imageView.image
                                                         ShowSize:self.cameraBtn.imageView.image.size
                                                         FileSize:200
                                                            block:^(NSData *resultData) {
             if(resultData) {
                 NSString *encodedImageStr = [resultData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
                 [self updateFace:encodedImageStr]; //上传服务器,这个观众们可以忽略我就不写这个方法了
               }
         }];
    }
    
    -(NSString *)removeSpaceAndNewline:(NSString *)str
    {
        NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
        temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
        temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        temp = [temp stringByReplacingOccurrencesOfString:@"=" withString:@"" options:NSBackwardsSearch range:NSMakeRange(str.length-3, 3)];
        return temp;
    }
    

    解析过滤之后的base64 检验是否是规范的base64 ,主要就是这个方法能够成功把一些不规范的base64转成图片

    //我自己写的方法 ,这边的str 就是Base64的字符串,命名不太好,各位勿喷
    - (UIImage *)stringToImage:(NSString *)str {
       NSData *imageData = [self newBase64Decode:str];
       UIImage *image = [UIImage imageWithData:imageData];
       return image;
    }
    
    //这个可以查看是否是规范的base64,并转成二进制的Data
    -(NSData*)newBase64Decode:(NSString *)string
    {
        unsigned long ixtext, lentext;
        unsigned char ch, inbuf[4], outbuf[4];
        short i, ixinbuf;
        Boolean flignore, flendtext = false;
        const unsigned char *tempcstring;
        NSMutableData *theData;
        if (string == nil) {
            return [NSData data];
        }
        ixtext = 0;
        tempcstring = (const unsigned char *)[string UTF8String];
        lentext = [string length];
        theData = [NSMutableData dataWithCapacity: lentext];
        ixinbuf = 0;
        while (true) {
            if (ixtext >= lentext){
                break;
            }
            ch = tempcstring [ixtext++];
            flignore = false;
            if ((ch >= 'A') && (ch <= 'Z')) {
                
                ch = ch - 'A';
                
            } else if ((ch >= 'a') && (ch <= 'z')) {
                
                ch = ch - 'a' + 26;
                
            } else if ((ch >= '0') && (ch <= '9')) {
                
                ch = ch - '0' + 52;
                
            } else if (ch == '+') {
                
                ch = 62;
                
            } else if (ch == '=') {
                
                flendtext = true;
                
            } else if (ch == '/') {
                
                ch = 63;
                
            } else {
                flignore = true;
            }
            if (!flignore) {
                
                short ctcharsinbuf = 3;
                
                Boolean flbreak = false;
                
                
                
                if (flendtext) {
                    
                    if (ixinbuf == 0) {
                        
                        break;
                        
                    }
                    
                    
                    
                    if ((ixinbuf == 1) || (ixinbuf == 2)) {
                        
                        ctcharsinbuf = 1;
                        
                    } else {
                        
                        ctcharsinbuf = 2;
                        
                    }
                    
                    
                    
                    ixinbuf = 3;
                    
                    
                    
                    flbreak = true;
                    
                }
                
                
                
                inbuf [ixinbuf++] = ch;
                
                
                
                if (ixinbuf == 4) {
                    
                    ixinbuf = 0;
                    
                    
                    
                    outbuf[0] = (inbuf[0] << 2) | ((inbuf[1] & 0x30) >> 4);
                    
                    outbuf[1] = ((inbuf[1] & 0x0F) << 4) | ((inbuf[2] & 0x3C) >> 2);
                    
                    outbuf[2] = ((inbuf[2] & 0x03) << 6) | (inbuf[3] & 0x3F);
                    
                    
                    
                    for (i = 0; i < ctcharsinbuf; i++) {
                        
                        [theData appendBytes: &outbuf[i] length: 1];
                        
                    }
                    
                }
                
                
                
                if (flbreak) {
                    
                    break;
                    
                }
                
            }
            
        }
        
        
        
        return theData;
        
    }
    
    

    相关文章

      网友评论

          本文标题:IOS 解析过滤之后的base64码

          本文链接:https://www.haomeiwen.com/subject/htuutktx.html