本文基本全部都是代码
首先是.H文件
1 #import <Foundation/Foundation.h> 2 3 @interface Base64AndImageHelp : NSObject 4 - (NSString*)encodeURL:(NSString *)string; 5 +(id)mydataWithBase64EncodedString:(NSString *)string ; 6 @end
然后是.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] == '