ios 在目标 c 中将文件作为附件发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4302403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:10:32 来源:igfitidea点击:
Send a file as attachment in objective c
提问by sandy
I want to mail a file (image) as an attachment by just picking it from an Image Picker. What is the appropriate way I can attach and mail a file (specifically, an image) in iOS Objective-C?
我想通过从图像选择器中挑选文件(图像)作为附件邮寄。我可以在 iOS Objective-C 中附加和邮寄文件(特别是图像)的合适方式是什么?
回答by Rog
Use the method below
使用下面的方法
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Check out this image!"];
// Set up recipients
// NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
// NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"];
// [picker setToRecipients:toRecipients];
// [picker setCcRecipients:ccRecipients];
// [picker setBccRecipients:bccRecipients];
// Attach an image to the email
UIImage *coolImage = ...;
NSData *myData = UIImagePNGRepresentation(coolImage);
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"coolImage.png"];
// Fill out the email body text
NSString *emailBody = @"My cool image is attached";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
And implement the delegate method
并实现委托方法
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(@"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Result: failed");
break;
default:
NSLog(@"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
And in your interface file
在你的界面文件中
#import <MessageUI/MFMailComposeViewController.h>
...
@interface ... : ... <MFMailComposeViewControllerDelegate>