Skip to content

Instantly share code, notes, and snippets.

@xhan
Created October 13, 2009 02:58
Show Gist options
  • Save xhan/208936 to your computer and use it in GitHub Desktop.
Save xhan/208936 to your computer and use it in GitHub Desktop.
apps in iphone send email with images
- (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;
}
- (void) emailButtonPressed:(id)sender {
NSString *body = @"<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'><html><head></head><body>";
NSData *imageData = nil ;
NSString* dataStr = nil ;
for (PhotoItem* item in _photoBoardView.itemsSelected) {
imageData = UIImageJPEGRepresentation(item.photo.image.image,0.9);
dataStr = [self base64EncodingWithLineLength:0 data:imageData];
body = [body stringByAppendingFormat:@"<b><img src='data:image/jpg;base64,%@' alt=' image'/></b>",dataStr];
}
body = [body stringByAppendingString:@"</body></html>"];
NSString *encoded = [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *atitle = [[@"" stringByAppendingFormat:@"title: %@", @"Image "] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString * urlString = [@"" stringByAppendingFormat:@"mailto:%@?subject=%@&body=%@", @"",atitle, encoded];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment