将照片添加到MFMailComposeViewDelegate(Xcode 4.2,iOS 5,ARC,LLDB,Storyboard,Universal App)

时间:2012-01-28 00:34:36

标签: iphone xcode ipad mfmailcomposeviewcontroller

我在互联网上搜索并堆叠,并没有找到适合我的条件的正确答案。 (Xcode 4.2,iOS 5,ARC,LLDB,Storyboard,Universal App)

寻找允许用户选择要添加到电子邮件中的照片的方法。

我希望保持非突兀,并在MFMailComposeViewController中添加一个按钮,允许用户从他们的照片库添加照片或用相机拍照。

我尝试使用一个按钮来查看带有UIImpagePicker的ViewController和视图控制器上的Camera,它使用一个单独的邮件功能按钮将图像添加到UIImageView,但是这样做太过分了,并且不必要地弄乱了应用程序,我也无法弄清楚代码将所选照片插入电子邮件。

我的inapp电子邮件工作正常,这是代码

·H

#import <MessageUI/MessageUI>
<MFMailComposeViewControllerDelegate>

的.m

- (IBAction)Email:(id)sender 
{
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];        
        mail.mailComposeDelegate = self;

        [mail setSubject:@"Feedback"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"feedback", nil];
        [mail setToRecipients:toRecipients];

        NSString *emailBody = @"-Your Message Here-";
        [mail setMessageBody:emailBody isHTML:NO];

        mail.modalPresentationStyle = UIModalPresentationPageSheet;

        [self presentModalViewController:mail animated:YES];

    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error:" 
                                                        message:@"E-mail is not supported on your device."
                                                       delegate:nil 
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:nil];
        [alert show];
    }
}

2 个答案:

答案 0 :(得分:1)

邮件编辑器不能自行添加用户选择的照片,也不建议在编辑器视图控制器中添加控件。
您必须首先使用UIImagePickerController让用户选择照片,然后在呈现邮件编辑器时将其添加为附件。

问题不在于如何编码,而在于如何设计UI。也许你可以让它有点像推特应用处理各种附件和附加信息。

费边

答案 1 :(得分:0)

但您无法访问照片库文件或所选文件的目录,无法作为邮件选择器的附件添加。

它必须像:

NSString * path = [[NSBundle mainBundle] pathForResource:@“arrest”ofType:@“png”]; NSData * myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@“image / png”fileName:@“arrest”];