我想通过从图像选择器中选取文件(图像)作为附件进行邮寄。
在 iOS Objective-C 中附加和邮寄文件(特别是图像)的正确方法是什么?
使用下面的方法
-(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] /cdn-cgi/l/email-protection"];
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected] /cdn-cgi/l/email-protection", @"[email protected] /cdn-cgi/l/email-protection", nil];
// NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected] /cdn-cgi/l/email-protection"];
// [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];
}
并实现委托方法
- (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];
}
在你的接口文件中
#import <MessageUI/MFMailComposeViewController.h>
...
@interface ... : ... <MFMailComposeViewControllerDelegate>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)