gpt4 book ai didi

ios - 在 objective-c 中将文件作为附件发送

转载 作者:IT王子 更新时间:2023-10-29 07:46:21 27 4
gpt4 key购买 nike

我想通过从图像选取器中选取一个文件(图像)作为附件来发送它。在 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:@"first@example.com"];
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
// NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];

// [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];
}

并实现委托(delegate)方法

- (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>

关于ios - 在 objective-c 中将文件作为附件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4302403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com