I have 10 images that are collected and imported into email and the user will send the images but they keep showing up as attachments and I need the images to be one behind the other and not as attachments. I was thinking of a pdf but I don't know how to generate one...
Here is my code:
- (IBAction)openMail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Contents Concepts Photo report"];
[mailer.view setNeedsDisplay];
NSString *path = [[NSBundle mainBundle] pathForResource:#"ContentsConceptsLogoFinal6copy" ofType:#"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailer addAttachmentData:myData mimeType:#"image/png" fileName:#"photo name"];
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"1" ofType:#"png"];
NSData *myData1 = [NSData dataWithContentsOfFile:path1];
[mailer addAttachmentData:myData1 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageData = UIImagePNGRepresentation(self.personimgThumbNail.image);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"Imageone"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"2" ofType:#"png"];
NSData *myData2 = [NSData dataWithContentsOfFile:path2];
[mailer addAttachmentData:myData2 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatatwo = UIImagePNGRepresentation(self.personimgThumbNailtwo.image);
[mailer addAttachmentData:imageDatatwo mimeType:#"image/png" fileName:#"Imagetwo"];
NSString *path3 = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"png"];
NSData *myData3 = [NSData dataWithContentsOfFile:path3];
[mailer addAttachmentData:myData3 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatathree = UIImagePNGRepresentation(self.personimgThumbNailthree.image);
[mailer addAttachmentData:imageDatathree mimeType:#"image/png" fileName:#"Imagethree"];
NSString *path4 = [[NSBundle mainBundle] pathForResource:#"4" ofType:#"png"];
NSData *myData4 = [NSData dataWithContentsOfFile:path4];
[mailer addAttachmentData:myData4 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatafour = UIImagePNGRepresentation(self.personimgThumbNailfour.image);
[mailer addAttachmentData:imageDatafour mimeType:#"image/png" fileName:#"Imagefour"];
NSString *path5 = [[NSBundle mainBundle] pathForResource:#"5" ofType:#"png"];
NSData *myData5 = [NSData dataWithContentsOfFile:path5];
[mailer addAttachmentData:myData5 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatafive = UIImagePNGRepresentation(self.personimgThumbNailfive.image);
[mailer addAttachmentData:imageDatafive mimeType:#"image/png" fileName:#"Imagefive"];
NSString *path6 = [[NSBundle mainBundle] pathForResource:#"6" ofType:#"png"];
NSData *myData6 = [NSData dataWithContentsOfFile:path6];
[mailer addAttachmentData:myData6 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatasix = UIImagePNGRepresentation(self.personimgThumbNailsix.image);
[mailer addAttachmentData:imageDatasix mimeType:#"image/png" fileName:#"Imagesix"];
NSString *path7 = [[NSBundle mainBundle] pathForResource:#"7" ofType:#"png"];
NSData *myData7 = [NSData dataWithContentsOfFile:path7];
[mailer addAttachmentData:myData7 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDataseven = UIImagePNGRepresentation(self.personimgThumbNailseven.image);
[mailer addAttachmentData:imageDataseven mimeType:#"image/png" fileName:#"Imageseven"];
NSString *path8 = [[NSBundle mainBundle] pathForResource:#"8" ofType:#"png"];
NSData *myData8 = [NSData dataWithContentsOfFile:path8];
[mailer addAttachmentData:myData8 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDataeight = UIImagePNGRepresentation(self.personimgThumbNaileight.image);
[mailer addAttachmentData:imageDataeight mimeType:#"image/png" fileName:#"Imageeight"];
NSString *path9 = [[NSBundle mainBundle] pathForResource:#"9" ofType:#"png"];
NSData *myData9 = [NSData dataWithContentsOfFile:path9];
[mailer addAttachmentData:myData9 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDatanine = UIImagePNGRepresentation(self.personimgThumbNailnine.image);
[mailer addAttachmentData:imageDatanine mimeType:#"image/png" fileName:#"Imagenine"];
NSString *path10 = [[NSBundle mainBundle] pathForResource:#"10" ofType:#"png"];
NSData *myData10 = [NSData dataWithContentsOfFile:path10];
[mailer addAttachmentData:myData10 mimeType:#"image/png" fileName:#"photo name"];
NSData *imageDataten = UIImagePNGRepresentation(self.personimgThumbNailten.image);
[mailer addAttachmentData:imageDataten mimeType:#"image/png" fileName:#"Imageten"];
[self presentModalViewController:mailer animated:YES];
}
}
The only explanation I can come up with is if you compile the images into one PDF, and attach the PDF. More on that here-
-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName: (NSString*)aFilename
{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];
// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[aView.layer renderInContext:UIGraphicsGetCurrentContext()];
// remove PDF rendering context
UIGraphicsEndPDFContext();
// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(#"documentDirectoryFileName: %#",documentDirectoryFilename);
}
Related
I am trying to generate a text file and attach it to email. Previously I used this code to attach the file to email and it is done successfully.
NSString *recipient = #"myemail#me.com";
NSArray *recipients = [NSArray arrayWithObjects:recipient, nil];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"Invalid Scan"];
[mailViewController setToRecipients:recipients];
[mailViewController setMessageBody:#"" isHTML:NO];
mailViewController.navigationBar.tintColor = [UIColor blackColor];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *textFilePath = [documentsDirectory stringByAppendingFormat:#"/textfile.txt"];
NSData *myData = [NSData dataWithContentsOfFile:textFilePath];
// NSLog(#"my nsdata is %#",myData);
[mailViewController addAttachmentData:myData
mimeType:#"text/csv"
fileName:textfile.txt];
But when I tried to use this statement below ,the file didn't get attached :
NSString *fileName_txtFile= #"textfile.txt";
NSString *textFilePath = [documentsDirectory stringByAppendingFormat:[NSString stringWithFormat:#"/%#",fileName_txtFile],nil];
NSData *myData = [NSData dataWithContentsOfFile:textFilePath];
[mailViewController addAttachmentData:myData
mimeType:#"text/csv"
fileName:fileName_txtFile];
I couldn't figure out, why the file is not attaching to the mail. Could you please help me in this issue?
Thanks in advance!!
I would like to add [self screenshot]; to my mail function as attachment. How can I do this without saving the image to the photo library? Here´s my code in ViewController.m:
Screenshot Function:
- (UIImage *)screenshot
{
UIImage *backgroundImage = [UIImage imageNamed:#"iphoneframe.png"];
UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
[screenshot drawInRect:CGRectMake(backgroundImage.size.width - screenshot.size.width, backgroundImage.size.height - screenshot.size.height, screenshot.size.width, screenshot.size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
E-Mail Function:
- (void)sendMail
{
if ([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Mein Punktestand bei KlickMich"];
//Attachement Object
UIImage *myImage = [UIImage imageNamed:#"image.jpg"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"image.jpg"];
NSString *messageBody = [NSString stringWithFormat:#"Hey, habe gerade ganze %i Punkte innerhalb von nur 15 Sekunden in der KlickMich App erreicht. Kannst du es besser?<br /><br />-> <a href='http://appstore.com/KlickMich'>appstore.com/KlickMich</a>",count];
[mailer setMessageBody:messageBody isHTML:YES];
[self presentViewController:mailer animated:YES completion:NULL];
}
[…]
Yours faithfully
Robin
When you take a screenshot that time you have to save your image in DocumentDirectory like this :
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath=[path objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager] ;
BOOL isDir ;
imagePath =[imagePath stringByAppendingPathComponent:#"MyImage"];
if(YES == [fileManager fileExistsAtPath:imagePath isDirectory:&isDir])
{
}
else
{
[fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:NO attributes:nil error:nil];
}
imagePath =[imagePath stringByAppendingPathComponent:#"Image.jpg"];
NSData *tempImageData=[NSData dataWithData:UIImagePNGRepresentation(viewImage)];
[tempImageData writeToFile:imagePath atomically:YES];
When you want to share that image you have to retrieve that image from DocumentDirectory & share it.
NSFileManager *fileManager = [NSFileManager defaultManager] ;
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath=[path objectAtIndex:0];
imagePath =[imagePath stringByAppendingPathComponent:#"MyImage"];
NSArray *temp=[fileManager contentsOfDirectoryAtPath:imagePath error:nil];
NSString *tempImgPath = [imagePath stringByAppendingPathComponent:[temp objectAtIndex:0]];
NSData *imgData = [NSData dataWithContentsOfFile:tempImgPath];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Mein Punktestand bei KlickMich"];
[mailer addAttachmentData:imgData mimeType:#"image/jpg" fileName:#"image.jpg"];
NSString *messageBody = [NSString stringWithFormat:#"Hey, habe gerade ganze %i Punkte innerhalb von nur 15 Sekunden in der KlickMich App erreicht. Kannst du es besser?<br /><br />-> <a href='http://appstore.com/KlickMich'>appstore.com/KlickMich</a>",count];
[mailer setMessageBody:messageBody isHTML:YES];
[self presentViewController:mailer animated:YES completion:NULL];
I am developing native iOS application.
I have a requirement as copy an image from my application to mail app, how can i accomplish the task kindly suggest me.
Thanks in Advance..
using MFMailComposeViewController
MFMailComposeViewController *mail=[MFMailComposeViewController alloc]init];
// Determine the file name and extension
NSString *extension =#"png";
NSString * filename=#"email";
// Get the resource path and read the file using NSData
NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
// Determine the MIME type
NSString *mimeType;
if ([extension isEqualToString:#"jpg"])
{
mimeType = #"image/jpeg";
}
else if ([extension isEqualToString:#"png"])
{
mimeType = #"image/png";
}
else if ([extension isEqualToString:#"doc"])
{
mimeType = #"application/msword";
}
else if ([extension isEqualToString:#"ppt"])
{
mimeType = #"application/vnd.ms-powerpoint";
}
else if ([extension isEqualToString:#"html"])
{
mimeType = #"text/html";
}
else if ([extension isEqualToString:#"pdf"])
{
mimeType = #"application/pdf";
}
// Add attachment
[mail addAttachmentData:fileData mimeType:mimeType fileName:filename];
Use MFMailComposerController and use this code:
MFMailComposeViewController * mcvc = [[MFMailComposeViewController alloc] init];
mcvc = self;
[mcvc setSubject:#"Subject"];
[mailer setToRecipients:#[#"mail#domain.com"]];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"mrweb" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
[mcvc addAttachmentData:imageData mimeType:#"image/png" fileName:#"allegato1.png"];
[mcvc setMessageBody:#"Hello bro !" isHTML:NO];
[self presentViewController:mcvc animated:YES completion:^{}];
If you have an UIImage use
NSData *imageData = UIImagePNGRepresentation(myImage.image);
to convert UIImage in NSData.
Here the complete list of all mime-types.
My question is what format the image is saved, if is dat or jpg. This is the code that i used:
NSString * urlImage = .....;
NSString * _folderPath = .....;
NSString * imageName = [[urlImage componentsSeparatedByString:#"/"] lastObject];
NSString * jpegPath = [NSString stringWithFormat:#"%#%#",_folderPath,imageName];
if (![[NSFileManager defaultManager] fileExistsAtPath:jpegPath])
{
NSURL *url = [NSURL URLWithString:urlImage];
//Download image
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];
//Save image
NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data writeToFile:jpegPath atomically:YES];
}
Following is piece of code for save .jpg image
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString* path = [docs stringByAppendingFormat:#"/image1.jpg"];
NSData* imageData = [NSData dataWithData:UIImageJPEGRepresentation(imageView.image, 80)];
NSError *writeError = nil;
[imageData writeToFile:path options:NSDataWritingAtomic error:&writeError];
You should use
stringByAppendingPathComponent method to create or get exact valid path
Use this way:
NSString * jpegPath = [_folderPath stringByAppendingPathComponent:imageName];// [NSString stringWithFormat:#"%#%#",_folderPath,imageName];
how to insert uiimage into body of mail composer window.. i have with this code:
NSMutableString *emailBody = [[[NSMutableString allocinitWithString:#""] retain];
[emailBody appendString:#" type text here"];
UIImage *emailImage = [UIImage imageNamed:#"20-gear2.png"]; NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
NSString *base64String = [imageData base64EncodedString];
[emailBody appendString:[NSString stringWithFormat:#"",base64String]];
NSLog(#"emailBody....%#", emailBody);
[emailBody appendString:#""];
NSLog(#"emailBody1....%#", emailBody);
MFMailComposeViewController *mailComposerController = [[MFMailComposeViewController alloc] init];
[mailComposerController setSubject:#"Your subject"];
[mailComposerController setMessageBody:nil isHTML:YES];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:"your image.png"]);
[mailComposerController addAttachmentData:imageData mimeType:#"image/png" fileName:#"image.png"];
Hope this help you...
NSData *imageData = UIImagePNGRepresentation(yourImage);
[mailController addAttachmentData:imageData mimeType:#"image/png" fileName:imageName];