IOS开发---菜鸟学习之路--(十四)-将BASE64图片转换成Image

本文基本全部都是代码

首先是.H文件

1 #import <Foundation/Foundation.h>
2 
3 @interface Base64AndImageHelp : NSObject
4 - (NSString*)encodeURL:(NSString *)string;
5 +(id)mydataWithBase64EncodedString:(NSString *)string ;
6 @end
Base64AndImageHelp.h

然后是.M文件

  1 #import "Base64AndImageHelp.h"
  2 @interface NSData (MBBase64)
  3 
  4 + (id)dataWithBase64EncodedString:(NSString *)string;
  5 - (NSString *)base64Encoding;
  6 
  7 @end
  8 static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  9 
 10 @implementation NSData (MBBase64)
 11 
 12 + (id)dataWithBase64EncodedString:(NSString *)string {
 13     if (string == nil)
 14         [NSException raise:NSInvalidArgumentException format:@""];
 15     
 16     if ([string length] == 0)
 17         return [NSData data];
 18     
 19     static char *decodingTable = NULL;
 20     
 21     if (decodingTable == NULL) {
 22         decodingTable = malloc(256);
 23         if (decodingTable == NULL)
 24             return nil;
 25         memset(decodingTable, CHAR_MAX, 256);
 26         NSUInteger i;
 27         for (i = 0; i < 64; i++)
 28             decodingTable[(short)encodingTable[i]] = i;
 29     }
 30     
 31     const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
 32     if (characters == NULL)
 33         return nil;
 34     char *bytes = malloc((([string length] + 3) / 4) * 3);
 35     if (bytes == NULL)
 36         return nil;
 37     
 38     NSUInteger length = 0;
 39     NSUInteger i = 0;
 40     
 41     while (YES) {
 42         char buffer[4];
 43         short bufferLength;
 44         for (bufferLength = 0; bufferLength < 4; i++) {
 45             if (characters[i] == '')
 46                 break;
 47             if (isspace(characters[i]) || characters[i] == '=')
 48                 continue;
 49             buffer[bufferLength] = decodingTable[(short)characters[i]];
 50             if (buffer[bufferLength++] == CHAR_MAX) {
 51                 free(bytes);
 52                 return nil;
 53             }
 54         }
 55         
 56         if (bufferLength == 0)
 57             break;
 58         if (bufferLength == 1) {
 59             free(bytes);
 60             return nil;
 61         }
 62         
 63         bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
 64         if (bufferLength > 2)
 65             bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
 66         if (bufferLength > 3)
 67             bytes[length++] = (buffer[2] << 6) | buffer[3];
 68     }
 69     
 70     realloc(bytes, length);
 71     return [NSData dataWithBytesNoCopy:bytes length:length];
 72 }
 73 
 74 - (NSString *)base64Encoding {
 75     if ([self length] == 0)
 76         return @"";
 77     
 78     char *characters = malloc((([self length] + 2) / 3) * 4);
 79     if (characters == NULL)
 80         return nil;
 81     
 82     NSUInteger length = 0;
 83     NSUInteger i = 0;
 84     
 85     while (i < [self length]) {
 86         char buffer[3] = {0,0,0};
 87         short bufferLength = 0;
 88         while (bufferLength < 3 && i < [self length])
 89             buffer[bufferLength++] = ((char *)[self bytes])[i++];
 90         characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
 91         characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
 92         if (bufferLength > 1)
 93             characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
 94         else characters[length++] = '=';
 95         if (bufferLength > 2)
 96             characters[length++] = encodingTable[buffer[2] & 0x3F];
 97         else characters[length++] = '=';
 98     }
 99     
100     return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] ;
101 }
102 
103 @end
104 @implementation Base64AndImageHelp
105 - (NSString*)encodeURL:(NSString *)string
106 {
107     NSString *newString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;="<>%{}|\^~`"),kCFStringEncodingUTF8));
108     if (newString) {
109         return newString;
110     }
111     return @"";
112     
113     
114 }
115 + (id)mydataWithBase64EncodedString:(NSString *)string {
116     if (string == nil)
117         [NSException raise:NSInvalidArgumentException format:@""];
118     
119     if ([string length] == 0)
120         return [NSData data];
121     
122     static char *decodingTable = NULL;
123     
124     if (decodingTable == NULL) {
125         decodingTable = malloc(256);
126         if (decodingTable == NULL)
127             return nil;
128         memset(decodingTable, CHAR_MAX, 256);
129         NSUInteger i;
130         for (i = 0; i < 64; i++)
131             decodingTable[(short)encodingTable[i]] = i;
132     }
133     
134     const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
135     if (characters == NULL)
136         return nil;
137     char *bytes = malloc((([string length] + 3) / 4) * 3);
138     if (bytes == NULL)
139         return nil;
140     
141     NSUInteger length = 0;
142     NSUInteger i = 0;
143     
144     while (YES) {
145         char buffer[4];
146         short bufferLength;
147         for (bufferLength = 0; bufferLength < 4; i++) {
148             if (characters[i] == '')
149                 break;
150             if (isspace(characters[i]) || characters[i] == '=')
151                 continue;
152             buffer[bufferLength] = decodingTable[(short)characters[i]];
153             if (buffer[bufferLength++] == CHAR_MAX) {
154                 free(bytes);
155                 return nil;
156             }
157         }
158         
159         if (bufferLength == 0)
160             break;
161         if (bufferLength == 1) {
162             free(bytes);
163             return nil;
164         }
165         
166         bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
167         if (bufferLength > 2)
168             bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
169         if (bufferLength > 3)
170             bytes[length++] = (buffer[2] << 6) | buffer[3];
171     }
172     
173     realloc(bytes, length);
174     return [NSData dataWithBytesNoCopy:bytes length:length];
175 }
176 @end
Base64AndImageHelp.m

完成该类后就可以在其他类中使用了

NSData *newimage=[Base64AndImageHelp mydataWithBase64EncodedString:base64的STRING];

UIImage *newjiaban=[[UIImage alloc] initWithData:newimage];

ok这样就完成拉

原文地址:https://www.cnblogs.com/PleaseInputEnglish/p/3486557.html