• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - Xcode : Add attachment using picker to email

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:09:47 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap