如何使用UIActivityViewController电子邮件共享设置电子邮件附件文件名

时间:2013-01-15 02:02:34

标签: iphone uiactivityindicatorview email-attachments

我正在尝试使用UIAcitivityViewController电子邮件共享来共享相机胶卷图像。

以下是我的代码。

 ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef iref = [rep fullScreenImage];
    UIImage *largeimage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:UIImageOrientationUp];
    NSData *imageData = UIImageJPEGRepresentation(largeimage, 1.0);
    NSArray *activityItems;

    if (largeimage != nil) {
        activityItems = @[imageData];
    }

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityController animated:YES completion:nil];

我不知道如何设置附件图像文件名。 有什么建议吗?

4 个答案:

答案 0 :(得分:5)

尝试在本地保存图片,然后发送文件网址,它肯定会显示您的图片名称。

ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullScreenImage];
UIImage *largeimage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:UIImageOrientationUp];
NSData *imageData = UIImageJPEGRepresentation(largeimage, 1.0);
// Store imageData locally
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filePath = [documentsPath stringByAppendingPathComponent:"@abc.jpg"];
[imageData writeToFile:filePath atomically:YES];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];

UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[fileUrl]
                                          applicationActivities:nil];
[self.navigationController presentViewController:activityViewController animated:YES completion:^{}];

答案 1 :(得分:0)

试试这个......

NSString *text = @"write text here";
UIImage *image = [UIImage imageNamed:@"your image name"];
NSArray *activityItems = [NSArray arrayWithObjects:text,image , nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems: activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];

答案 2 :(得分:0)

您可以像这样制作自己的相关内容,例如您可以在此链接中找到:https://github.com/nferocious76/NFActivityModel只需使用它,您也可以添加密送,抄送收件人。

NSURL *fileURL = [NSURL URLWithString:pdfFileURL];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfFileURL];

NFActivityShareMailIAttachment *shareMailAttachment = [[NFActivityShareMailIAttachment alloc] initWithAttachmentData:pdfData attachmentDataMimeType:@"application/pdf" attachmentDataFilename:pdfFilename];

NSString *taskName = self.reportInfo[@"TaskName"] ?: @"PDF Share";
NSString *sharingUser = self.reportInfo[@"SharingUser"] ?: @"JV User";

// define ShareMail item
NSString *mailBody = @"ShareMail Body";
NFActivityShareMailItem *activityShareMailItem = [[NFActivityShareMailItem alloc] initWithShareMailSubject:taskName mailBody:mailBody isHTML:NO];
activityShareMailItem.attachments = @[shareMailAttachment];

// create ShareMail with the base view controller
NFActivityShareMail *shareMail = [[NFActivityShareMail alloc] initWithViewController:self];

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[pdfData, fileURL, activityShareMailItem] applicationActivities:@[shareMail]];
activityViewController.excludedActivityTypes = @[UIActivityTypeMail];

// present to user
[self presentViewController:activityViewController animated:YES completion:nil];

答案 3 :(得分:-1)

在您的分享方法中尝试此代码..

// add all objects in array which you want to attach. 
    NSArray *dataToShare =[[NSArray alloc] initWithObjects:title.text,notes.text,image,nil];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
    [self presentViewController:activityVC animated:YES completion:^{

            }];