【问题标题】:Attach a photo to an email from my iPhone application从我的 iPhone 应用程序将照片附加到电子邮件
【发布时间】:2012-10-19 03:26:57
【问题描述】:

我有一个 iPhone 应用程序,它从 App 内置的相册中挑选照片。现在我想添加一个分享按钮,可以选择通过电子邮件分享这张照片,我可以通过这个代码附加一张现有的照片:

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@""];


// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@""]; 
[picker setToRecipients:toRecipients];


// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"project existing photo" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"photo name"];

// Fill out the email body text
NSString *emailBody = @"";
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
[picker release];

但是我需要在此代码中进行哪些更改才能将选择的相册附加到电子邮件正文中? 提前致谢。

【问题讨论】:

  • “将挑选的相册附加到邮件正文中”是什么意思?
  • 我有一个按钮,用户可以从我需要的照片应用程序中选择一张照片,以便用户能够将该照片附加到电子邮件正文中。

标签: iphone ios xcode email-attachments file-sharing


【解决方案1】:

使用UIImagePickerController 允许用户选择图像。然后它会调用这个委托方法。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData* data = UIImageJPEGRepresentation(image, 1.0);
    // Your e-mail code here
}

【讨论】:

  • 非常感谢,你救了我的一天。你知道这是否适用于短信吗?我在网上搜索过,但没有找到任何帮助。
  • 无法在应用内发送彩信。您所能做的就是复制图像并启动可以粘贴照片的消息应用程序。
  • 那么苹果照片App是怎么做到的呢?他们不让开发人员做他们能做的事。
  • 您能否提供一个用于将照片复制并粘贴到彩信的示例代码。提前致谢。
【解决方案2】:

您好,使用 UIImagePicker 从相机的 PhotoLibrary 中选择图像并使用 MFMailComposeViewController 发送电子邮件。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

// Dismiss PickerViewController 

[picker dismissModalViewControllerAnimated:NO]; 

// Get Image Fro Attachment

UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData* data = UIImageJPEGRepresentation(image, 1.0);

// Setup Email Settings Like Subject, Message , Attachment


MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;

[mailPicker setSubject:@"Image Attachment Test"];


// Set recipients

NSArray *toRecipients = [NSArray arrayWithObject:@"xyz.gmail.com"]; 
[mailPicker setToRecipients:toRecipients];

// Set body message here
NSString *emailBody = @":)";
[picker setMessageBody:emailBody isHTML:NO];

// Attach Image as Data 
[mailPicker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"photo name"];

[self presentModalViewController:mailPicker animated:YES];

[mailPicker release];


}

【讨论】:

    【解决方案3】:

    假设您有一个来自图像选择器(或任何其他来源)的 UIImage,您首先需要从图像创建一个 NSData 对象。使用 UIImageJPEGRepresentationUIImageJPEGRepresentation 函数。获得 NSData 对象后,将其添加为附件,就像您在发布的代码中所做的那样。

    在大多数情况下,图像会出现在邮件正文之后。

    【讨论】:

    • 感谢您的回答。您知道这是否适用于短信吗?我在网上搜索过,但没有找到任何帮助。
    • 这是一个单独的问题。在 SO 上发布一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多