OStack程序员社区-中国程序员成长平台

标题: ios - Xcode : Add attachment using picker to email [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:09
标题: ios - Xcode : Add attachment using picker to email

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

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)chooseid)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)chooseid)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc] initWithObjectsNSString *)kUTTypeMovie,      nil];

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


 - (void)imagePickerControllerUIImagePickerController *)picker didFinishPickingMediaWithInfoNSDictionary *)info {


UIImage *chosenImage = info[UIImagePickerControllerEditedImage];


[self performSelectorselector(email withObject:chosenImage afterDelay:0.5];

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancelUIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)emailUIImage *)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: @"[email protected]",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: @"lease 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)mailComposeControllerMFMailComposeViewController *)controller didFinishWithResultMFMailComposeResult)result errorNSError *)error
{
 [self dismissViewControllerAnimated:YES completion:nil];
}

@end

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



Best Answer-推荐答案


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

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

用户没有选择图片 - 他们选择了视频。你需要改用这个:

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

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

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4