zoukankan      html  css  js  c++  java
  • IOS开发---菜鸟学习之路--(十六)-将Image转换为Base64

    我们直接在.m文件的引用头文件部分 和 @interface   AddPictureViewController ()

     之间  加入 增加部分的代码

    然后就可以使用图片转Base64了

    #import "AddPictureViewController.h"

    #import <QuartzCore/QuartzCore.h>

    #import <QuartzCore/CoreAnimation.h>

    #import <MobileCoreServices/UTCoreTypes.h>

      1 @interface NSData (MBBase64)
      2 
      3 + (id)dataWithBase64EncodedString:(NSString *)string;
      4 - (NSString *)base64Encoding;
      5 
      6 @end
      7 static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
      8 
      9 @implementation NSData (MBBase64)
     10 
     11 + (id)dataWithBase64EncodedString:(NSString *)string {
     12     if (string == nil)
     13         [NSException raise:NSInvalidArgumentException format:@""];
     14     
     15     if ([string length] == 0)
     16         return [NSData data];
     17     
     18     static char *decodingTable = NULL;
     19     
     20     if (decodingTable == NULL) {
     21         decodingTable = malloc(256);
     22         if (decodingTable == NULL)
     23             return nil;
     24         memset(decodingTable, CHAR_MAX, 256);
     25         NSUInteger i;
     26         for (i = 0; i < 64; i++)
     27             decodingTable[(short)encodingTable[i]] = i;
     28     }
     29     
     30     const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
     31     if (characters == NULL)
     32         return nil;
     33     char *bytes = malloc((([string length] + 3) / 4) * 3);
     34     if (bytes == NULL)
     35         return nil;
     36     
     37     NSUInteger length = 0;
     38     NSUInteger i = 0;
     39     
     40     while (YES) {
     41         char buffer[4];
     42         short bufferLength;
     43         for (bufferLength = 0; bufferLength < 4; i++) {
     44             if (characters[i] == '')
     45                 break;
     46             if (isspace(characters[i]) || characters[i] == '=')
     47                 continue;
     48             buffer[bufferLength] = decodingTable[(short)characters[i]];
     49             if (buffer[bufferLength++] == CHAR_MAX) {
     50                 free(bytes);
     51                 return nil;
     52             }
     53         }
     54         
     55         if (bufferLength == 0)
     56             break;
     57         if (bufferLength == 1) {
     58             free(bytes);
     59             return nil;
     60         }
     61         
     62         bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
     63         if (bufferLength > 2)
     64             bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
     65         if (bufferLength > 3)
     66             bytes[length++] = (buffer[2] << 6) | buffer[3];
     67     }
     68     
     69     realloc(bytes, length);
     70     return [NSData dataWithBytesNoCopy:bytes length:length];
     71 }
     72 
     73 - (NSString *)base64Encoding {
     74     if ([self length] == 0)
     75         return @"";
     76     
     77     char *characters = malloc((([self length] + 2) / 3) * 4);
     78     if (characters == NULL)
     79         return nil;
     80     
     81     NSUInteger length = 0;
     82     NSUInteger i = 0;
     83     
     84     while (i < [self length]) {
     85         char buffer[3] = {0,0,0};
     86         short bufferLength = 0;
     87         while (bufferLength < 3 && i < [self length])
     88             buffer[bufferLength++] = ((char *)[self bytes])[i++];
     89         characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
     90         characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
     91         if (bufferLength > 1)
     92             characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
     93         else characters[length++] = '=';
     94         if (bufferLength > 2)
     95             characters[length++] = encodingTable[buffer[2] & 0x3F];
     96         else characters[length++] = '=';
     97     }
     98     
     99     return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] ;
    100 }
    101 
    102 @end
    增加部分的代码

    @interface   AddPictureViewController ()

    @end

    具体的转换操作如下

     NSData *mydata=UIImageJPEGRepresentation(image , 0.4);

     NSString *pictureDataString=[mydata base64Encoding];

      

    如果需要传递到后台的话尽量进行编码操作

    编码部分的方法如下

    1 - (NSString*)encodeURL:(NSString *)string
    2 {
    3     NSString *newString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;="<>%{}|\^~`"),kCFStringEncodingUTF8));
    4     if (newString) {
    5         return newString;
    6     }
    7     return @"";
    8 }
    encodeURL

    然后我们只需要像这样子使用就可以了

    1 -(void)zhuanhuan:(id)sender
    2 {
    3     NSData *mydata=UIImageJPEGRepresentation(contentimageview.image , 0.4);
    4     NSString *pictureDataString=[mydata base64Encoding];
    5     NSString *image=[self encodeURL:pictureDataString];
    6    // NSLog(pictureDataString);
    7 }
    View Code

    这边再给大家提供一个在线转换工具大家可以直接在上面进行Image与Base64之间的转换

    http://www.vgot.net/test/image2base64.php

  • 相关阅读:
    WPF线程中获取控件的值和给控件赋值
    sublime text3 安装以及主要插件安装
    云服务IaaS,PaaS,SaaS
    What is JSON
    Core Java
    英语单词及语义
    设置PyCharm创建文件时自动添加头文件
    【练习】字典的循环遍历:实现多层级节点存取
    字符串格式化
    常用数据类型的方法--str、int、list、dict
  • 原文地址:https://www.cnblogs.com/PleaseInputEnglish/p/3486561.html
Copyright © 2011-2022 走看看