gpt4 book ai didi

ios - 将图像附加到电子邮件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:31 24 4
gpt4 key购买 nike

我想知道是否有一种方法可以让用户从相机胶卷中选择一张图片,然后将其附加到电子邮件中?

这是我现在的代码:

-(IBAction) openEmail {

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:@"TPsecondary_Example@email.com", nil]];
[mailComposer setSubject:@"Learning Trail Submission"];
[mailComposer setMessageBody:emailbody isHTML:NO];
[mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Answer" ofType:@"plist"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailComposer addAttachmentData:myData mimeType:@"application/xml" fileName:@"Answer.plist"];

[self presentModalViewController:mailComposer animated:YES];
}
}

最佳答案

当然有!

在您的 .h 文件中添加这些委托(delegate)并声明一个名为 selectedImage 的 UIImage

<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

然后在您的 .m 中您可以添加以下内容。

-(IBAction)openImagePicker:(id)sender 链接到您要启动该过程的按钮。

- (IBAction)openImagePicker:(id)sender
{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^{
[self openEmail];
}];

}

-(IBAction) openEmail {

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:@"TPsecondary_Example@email.com", nil]];
[mailComposer setSubject:@"Learning Trail Submission"];
[mailComposer setMessageBody:emailbody isHTML:NO];
[mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Answer" ofType:@"plist"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailComposer addAttachmentData:myData mimeType:@"application/xml" fileName:@"Answer.plist"];
NSData *imageData = UIImageJPEGRepresentation(selectedImage, 1.0);
[mailComposer addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"imageTitle"];
[self presentModalViewController:mailComposer animated:YES];
}
}

编辑: 请注意,这是一个非常基本的示例,不处理用户选择视频而不是图像等事件...

关于ios - 将图像附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11998610/

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