Ios Xcode:使用选择器向电子邮件添加附件

Ios Xcode:使用选择器向电子邮件添加附件,ios,uiimagepickercontroller,email-attachments,Ios,Uiimagepickercontroller,Email Attachments,我开发了一个应用程序,允许用户从照片库中选择视频,并将其作为电子邮件附件发送。我可以从图库中选择一个视频并继续发送电子邮件,但视频没有与电子邮件连接。控制台中没有错误 ViewController.h: #import <UIKit/UIKit.h> #import <MobileCoreServices/MobileCoreServices.h> #import <MessageUI/MessageUI.h> @interface ViewControll

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

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

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

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

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

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

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

如果您需要同时附加视频和图像,您必须为这两个文件编写写入代码,但您只编写了用于附加图像的代码。您可以尝试下面的代码来获取这两个文件

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

    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqual:(NSString *)kUTTypeMovie]) {
        NSString *videoURL = info[UIImagePickerControllerMediaURL];
        [self emailImage:nil orVideo:videoURL];
    }else {
        UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
        [self emailImage:chosenImage orVideo:nil];
    }
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
uiImagePickerControllerDiaUrl
将返回不同于
UIImagePickerControllerEditedImage
的文件url,因此可以使用
NSData
方法
dataWithContentsOfFile

if (choosenImage) {
    NSData *data = UIImagePNGRepresentation(choosenImage);
    NSString *filename = [NSString stringWithFormat:@"Image_%@.png",TimeStamp];
    [mailComposer addAttachmentData:data mimeType:@"image/png" fileName:filename];
    [self presentViewController:mailComposer animated:YES completion:nil];
}else {
    NSData *data = [NSData dataWithContentsOfFile:videoFile];
    NSString *filename = [NSString stringWithFormat:@"Video_%@.mp4",TimeStamp];
    [mailComposer addAttachmentData:data mimeType:@"video/mp4" fileName:filename];
    [self presentViewController:mailComposer animated:YES completion:nil];
}
这将是很好的,如果你给一个附件的文件名,它将帮助下载后完全。如果你愿意,你可以使用一个时间戳

#define TimeStamp [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000]

我添加了u所说的编码..但我得到了错误:不兼容的指针类型在NSData*videoData=[NSData DATA WITHCONTENTSOFURL:chosenVideoUrl]处向类型为“NSURL*\u NONULL”的参数发送“UIImage*”;。。。我设法解决了它。我不得不改变:-(void)email:(UIImage)choosenImage{to-(void)email:(NSURL)choosenImage{非常感谢您的帮助。@Ryan Laneve听起来您错过了更新一个UIImage用法。一旦您进行了更改,就不应该有任何更新。是的..删除了所有UIImage..现在工作正常..谢谢:)@Ryan LeNeveThank@Shabeer Ali..这很有帮助。
if (choosenImage) {
    NSData *data = UIImagePNGRepresentation(choosenImage);
    NSString *filename = [NSString stringWithFormat:@"Image_%@.png",TimeStamp];
    [mailComposer addAttachmentData:data mimeType:@"image/png" fileName:filename];
    [self presentViewController:mailComposer animated:YES completion:nil];
}else {
    NSData *data = [NSData dataWithContentsOfFile:videoFile];
    NSString *filename = [NSString stringWithFormat:@"Video_%@.mp4",TimeStamp];
    [mailComposer addAttachmentData:data mimeType:@"video/mp4" fileName:filename];
    [self presentViewController:mailComposer animated:YES completion:nil];
}
#define TimeStamp [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000]