将照片添加到 MFMailComposeViewDelegate(Xcode 4.2、iOS 5、ARC、LLDB、Storyboard、通用应用程序)
我已经在互联网和堆栈上进行了搜索,但还没有找到适合我的情况的正确答案。 (Xcode 4.2、iOS 5、ARC、LLDB、Storyboard、Universal App)
寻找允许用户选择要添加到电子邮件的照片的方法。
我想保持它不显眼,并在 MFMailComposeViewController 中添加一个按钮,允许用户从照片库添加照片或使用相机拍照。
我尝试在视图控制器上使用带有 UIImpagePicker 和相机的按钮来查看控制器,该按钮通过单独的邮件功能按钮将图像添加到 UIImageView 中,但这太过分了,并且不必要地使应用程序混乱,我也无法找出插入代码选择照片到电子邮件中。
我的应用程序内电子邮件运行良好,这是代码
.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];
}
}
I have searched around the internet and stack and have not quite found the right answer for my conditions. ( Xcode 4.2, iOS 5, ARC, LLDB, Storyboard, Universal App )
Looking for the method to allow users the option to select a photo to add to the email.
I want to keep it non-obtrusive and add a button within MFMailComposeViewController that allows users to add a photo from their photo library or take a photo with their camera.
I tried to using a button to viewController with UIImpagePicker and Camera on view controller that added image to UIImageView with a separate mail function button, but this was overkill and clutter the app unnecessarily, I also was unable to figure out the to code to insert the chosen photo into the email.
My inapp email works perfectly, here is the code
.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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
邮件编辑器不提供添加用户选择的照片本身的功能,并且不建议向编辑器视图控制器添加控件。
您必须让用户首先使用 UIImagePickerController 选择照片,然后在显示邮件编辑器时将其添加为附件。
问题不在于如何编码,而在于如何设计 UI。也许你可以让它有点像 Twitter 应用程序处理各种附件和附加信息。
法比安
The mail composer doesn't offer the ability to add user selected photos itself and adding controls to the composer view controller isn't recommended.
You'll have to make the user choose the photo first using UIImagePickerController and then add it as an attachment when you present the mail composer.
The question is not how to code it, but rather how to design the UI. Maybe you could make it a little bit like the twitter app handles various attachment and additional information.
Fabian
但您无法访问照片库文件或所选文件的目录以作为附件添加到邮件选择器。
它必须是这样的:
NSString *path = [[NSBundle mainBundle] pathForResource:@"arrest" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"arrest"];
but you cant access the photo library files or the directory of your selected file for adding as an attachment to mail picker.
it has to be like :
NSString *path = [[NSBundle mainBundle] pathForResource:@"arrest" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"arrest"];