ios - Xcode : Add attachment using picker to email

标签 ios uiimagepickercontroller email-attachments

我开发了一个应用程序,允许用户从照片库中选择视频并将其作为电子邮件附件发送。我可以从图库中选择一个视频并继续发送电子邮件,但该视频不会附加到电子邮件中。控制台中没有错误。

ViewController.h:

#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate,MFMailComposeViewControllerDelegate>

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)choose:(id)sender;


@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![UIImagePickerController  isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

    UIAlertController *myAlertController = [UIAlertController  alertControllerWithTitle:@"MyTitle"
                                                                                message: @"MyMessage"
                                                                          preferredStyle:UIAlertControllerStyleAlert                   ];

    [self presentViewController:myAlertController animated:YES completion:nil];

}

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)choose:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,      nil];

[self presentViewController:picker animated:YES completion:NULL];
 }


 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


UIImage *chosenImage = info[UIImagePickerControllerEditedImage];


[self performSelector:@selector(email:) withObject:chosenImage afterDelay:0.5];

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)email:(UIImage *)choosenImage{
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
NSString *model = [[UIDevice currentDevice] model];
NSString *version = @"1.0";
NSString *build = @"100";
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:[NSArray arrayWithObjects: @"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8abada8a8b7aaac98b5a1b9a8a8afb7aab3abf6bbb7b5" rel="noreferrer noopener nofollow">[email protected]</a>",nil]];
[mailComposer setSubject:[NSString stringWithFormat: @"MailMe V%@ (build %@) Support",version,build]];
NSString *supportText = [NSString stringWithFormat:@"Device: %@\niOS Version:%@\n\n",model,iOSVersion];
supportText = [supportText stringByAppendingString: @"Please describe your problem or question."];
[mailComposer setMessageBody:supportText isHTML:NO];

NSData *data = UIImagePNGRepresentation(choosenImage);


[mailComposer addAttachmentData:data mimeType:@"image/png" fileName:@""];
[self presentViewController:mailComposer animated:YES completion:nil];

}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
 [self dismissViewControllerAnimated:YES completion:nil];
}

@end

任何建议/帮助将不胜感激。谢谢。

最佳答案

您提到您正在尝试附加视频,并且您已将 UIImagePickerController 配置为将媒体类型限制为仅视频。那么问题是您在“didFinishPickingMediaWithInfo”方法中请求“编辑后的图像”:

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

用户没有选择图像 - 他们选择了视频。您需要改用它:

NSURL *chosenVideoUrl = info[UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:chosenVideoUrl];

然后,您可以将视频数据传递到您的电子邮件方法并附加到电子邮件中。请务必将 mimeType 从“image/png”更新为“video/mp4”。

关于ios - Xcode : Add attachment using picker to email,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33748073/

相关文章:

ios - RestKit 附加数据响应

iphone - Xcode 4.2 中烦人的代码签名错误/警告

c# - 在MailMessage中添加Attachment base64图片,并在html body中读取

ios - 无法分配给属性 : 'outputs' is a get-only property

ios - SCNNode方向问题

ios - 如何将选定的图像从 UIImagePickerController 传递到第二个 View Controller ?

ios - 如何更改 UIImagePickerController TableView 的背景颜色?

ios - 如何在 Swift 中获取图像文件大小?

java - 无法使用 JavaMailAPI 发送带附件的邮件

某些电子邮件无法访问 PHP IMAP 电子邮件附件