Created
October 13, 2009 02:58
-
-
Save xhan/208936 to your computer and use it in GitHub Desktop.
apps in iphone send email with images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSString *) base64EncodingWithLineLength:(unsigned int) lineLength data:(NSData *)imgData { | |
static const char *encodingTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
const unsigned char *bytes = [imgData bytes]; | |
NSMutableString *result = [NSMutableString stringWithCapacity:[imgData length]]; | |
unsigned long ixtext = 0; | |
unsigned long lentext = [imgData length]; | |
long ctremaining = 0; | |
unsigned char inbuf[3], outbuf[4]; | |
short i = 0; | |
short charsonline = 0, ctcopy = 0; | |
unsigned long ix = 0; | |
while( YES ) { | |
ctremaining = lentext - ixtext; | |
if( ctremaining <= 0 ) break; | |
for( i = 0; i < 3; i++ ) { | |
ix = ixtext + i; | |
if( ix < lentext ) inbuf[i] = bytes[ix]; | |
else inbuf [i] = 0; | |
} | |
outbuf [0] = (inbuf [0] & 0xFC) >> 2; | |
outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4); | |
outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6); | |
outbuf [3] = inbuf [2] & 0x3F; | |
ctcopy = 4; | |
switch( ctremaining ) { | |
case 1: | |
ctcopy = 2; | |
break; | |
case 2: | |
ctcopy = 3; | |
break; | |
} | |
for( i = 0; i < ctcopy; i++ ) | |
[result appendFormat:@"%c", encodingTable[outbuf[i]]]; | |
for( i = ctcopy; i < 4; i++ ) | |
[result appendFormat:@"%c",'=']; | |
ixtext += 3; | |
charsonline += 4; | |
if( lineLength > 0 ) { | |
if (charsonline >= lineLength) { | |
charsonline = 0; | |
[result appendString:@"\n"]; | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment