6

I am trying to share camera roll image using UIAcitivityViewController email sharing.

Following are my code.

 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];

I don't know how can I set attachment image file name. Any advice?

ttotto
  • 826
  • 3
  • 13
  • 31

4 Answers4

5

Try saving image locally, then send the file url instead, it will show your image name surely.

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:^{}];
thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
  • it does not work. Sharing an UIImage simply "hides" the attachment name but does not let you to change it. – roberto.buratti Jun 13 '14 at 15:28
  • @roberto.buratti: It does work, if you first store the file/image locally. See [here](https://thomasguenzel.com/blog/2015/04/16/uiactivityviewcontroller-nsdata-with-filename/) for example. – testing Sep 26 '16 at 08:31
1

try this...

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];
Vikas Pandey
  • 550
  • 4
  • 13
  • it does not work. Sharing an UIImage simply "hides" the attachment name but does not let you to change it. – roberto.buratti Jun 13 '14 at 15:27
  • the question is 'How to set email attachment file name using UIActivityViewController email sharing' and your answer answer's how to add attachment. – eNeF Apr 11 '17 at 07:05
0

You can just make your own like this for example which you can find in this link: https://github.com/nferocious76/NFActivityModel just go play with it you can also add bcc,cc recipients.

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];
eNeF
  • 3,241
  • 2
  • 18
  • 41
-1

try this code in your share method..

// 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:^{

            }];
Vikas Pandey
  • 550
  • 4
  • 13