我们直接在.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] == '\0') 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