gpt4 book ai didi

ios - 代码 : Add attachment using picker to email

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:27 25 4
gpt4 key购买 nike

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

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: @"support@myappworks.com",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 配置为将 mediaTypes 限制为仅视频。那么问题是你在“didFinishPickingMediaWithInfo”方法中要求“编辑图像”:

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

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

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

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

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

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