呼吸de艺术

Live in your world, get owned in mine

让iPhoneApp发送带图片附件的邮件

| Comments

更新(Nov 9,2009):

在3.0系统中,sdk 提供了 MFMailComposeViewController 来显示发件界面,并提供附件功能。

if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController mcvc = [[[MFMailComposeViewController alloc] init] autorelease]; mcvc.mailComposeDelegate = self; [mcvc setSubject:@“Here’s a great photo!”]; NSString body = @“

Check this out

I selected this image from the UIImagePickerController.

”; [mcvc setMessageBody:body isHTML:YES]; [mcvc addAttachmentData:UIImageJPEGRepresentation(image, 1.0f) mimeType:@“image/jpeg” fileName:@“pickerimage.jpg”]; [self presentModalViewController:mcvc animated:YES]; }

在程序中如何启动系统的Email程序并在内容中添加图片附件? 以下是代码:

[c] – (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; } [/c]

[c] – (void) emailButtonPressed:(id)sender {

NSString *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:@“ image”,dataStr]; } body = [body stringByAppendingString:@“”];

NSString encoded = [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString atitle = [[@“” stringByAppendingFormat:@“title: %@”, @“Image ”] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString * urlString = [@“” stringByAppendingFormat:@“%@?subject=%@&amp;body=%@”, @“”,atitle, encoded]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; } [/c]

Comments