I am using the MessageUI framework to send and image via e-mail after taking the photo using UIImagePickerController. When I take the photo and then invoke the mail mail message interface I get the compose window. When testing on an iPod touch (iOS 4.3.5) and iPad (iOS 5.0.1) I see the image attachment in the body of the compose window. When testing on an iPhone (4S iOS 5.0.1) the image does not appear in the compose window, but rather I see a box the size of the image attachment with an embedded small blue box with a "?" in it.
In both cases when the mail message is sent the image appears in the message received in the Mail app - iOS devices and Mac.
What can I do to fix this?
UPDATE: I've worked around this by changing:
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(imageToSend)]
to:
NSData *imageDataJPG = [NSData dataWithData:UIImageJPEGRepresentation(imageToSend, 1.0)];
I can't see in the UIKit docs that there is anything in UIImagePNGRepresentation that would not work on an iPhone ...
(Xcode 4.2.1, ARC, 5.0 SDK, Deploy target 4.3)
Here is the code for composing the message:
-(void)displayComposerSheet
{
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
[mailPicker setSubject:#"Photo"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(imageToSend)];
[mailPicker addAttachmentData:imageData mimeType:#"image/png" fileName:#"Photo"];
// Fill out the email body text.
NSString *line1 = [NSString stringWithFormat: #"I took this photo on my %#.\n", [[UIDevice currentDevice] model]];
NSString *body = [NSString stringWithFormat:#"%#", line1];
[mailPicker setMessageBody:body isHTML:NO];
// Present the mail composition interface.
[self presentModalViewController:mailPicker animated:YES];
}
The image size is restricted so if the image you are sending through is greater than a certain dimension you'll get the effect you describe above.
I had a look at my own code and saw I had
#define MAX_MAIL_DIM 1536
Which seems to be 1024 * 1.5. Sorry I can't remember how I arrived at that number but I suspect it was trial and error.
larik, your suggestion about using JPEG for the data type worked great. PNG files at this size are way too big anyway -- around 10MB. Here's the code with the JPEG NSData:
if ([MFMailComposeViewController canSendMail]) {
picker = [[MFMailComposeViewController alloc] init];
[picker setMailComposeDelegate:self];
[picker setSubject:#"My Picture"];
NSString *emailBody = #"";
[picker setMessageBody:emailBody isHTML:YES];
NSData *data = UIImageJPEGRepresentation(tempImage, 0);
[picker addAttachmentData:data mimeType:#"image/jpg" fileName:#"CameraImage"];
}
Related
I am creating an app that makes use of iMessage and MMS.
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer) {
The problem is, after I take a picture then click the send button, the image randomly becomes a question mark.
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
The above code was the first the one we used followed by:
[self sendSMSmessage:myMessage image:imageData];
next, in the sendSMSmessage method, the following codes were called:
MFMessageComposeViewController *myText = [[MFMessageComposeViewController alloc] init];
[myText setMessageComposeDelegate:self];
[myText setBody:myMessage];
[myText addAttachmentData:image typeIdentifier:#"image/jpeg" filename:#"image.jpeg"];
Then, I present the MFMessageComposeViewController myText
[self presentViewController:myText animated:YES completion:nil];
Then, I clicked Send... It successfully sent for the app and I can see the picture on MFMessageComposeViewController. But when I try to look at iMessage, some of the pictures I tried to send is good. But some are not. Some show question mark. Did the images got corrupted in the process or what? I tried to compress the image using the following:
CGFloat compressionQuality = 0.3;
NSData *imageData = [NSData dataWithData:(UIImageJPEGRepresentation
([UIImage imageWithData:[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]]
, compressionQuality))];
but still performs the same. In 10 tries, I get something like 4 failed images. Could it be a problem in iPhone or just my app itself? Thanks!
I don't know if some people would experience this, but the thing I did to fix this was:
Declaring the imageData as a strong reference:
__strong NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
And then I changed the typeIdentifier of the myText composer into #"public.jpeg" instead of #"image/jpeg"
[myText addAttachmentData:image typeIdentifier:#"public.jpeg" filename:#"image.jpeg"];
I have an iOS app that should allow the user to select a pdf file and send it via there email account, I have the below code that presents the user with the email and shows the pdf attached, however with the email is send the pdf is not attached to the received email.
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Public Holidays"];
NSString *plistFilePath = [[NSBundle mainBundle] pathForResource:currentCountry ofType:#"pdf"];
NSData *myData = [[NSFileManager defaultManager] contentsAtPath:plistFilePath];
[picker addAttachmentData:myData mimeType:#"application/pdf" fileName:currentCountry];
// Fill out the email body text
NSString *emailBody = #"Attached to this email is the PDF bought";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
Code is correct. You should check the filename. Is it a valid string(filename). Also presentModalViewController is deprecated. You should use presentViewController:animated:completion. Check this on device, not on simulator.
I have an APP to share people's photos to Mail, but on iPhone 6 Plus, I found it's preview image sometimes got cut, like this :
Image link is here:
Broken Image
In fact, there is a string like "Send from my phone" after image, it is cut too.
But if I move cursor to the end of the image, and press return on the keyboard, the image and "Send from my phone" become intactness.
I'm sorry, I can't send image directly,
here are the links:
Move Cursor
Here is my code:
- (void) shareToMailWithALAsset:(ALAsset *) asset {
if ([MFMailComposeViewController canSendMail]) {
NSString *imageName = asset.defaultRepresentation.filename;
MFMailComposeViewController *mailVC = [MFMailComposeViewController new];
mailVC.mailComposeDelegate = self;
[mailVC setMessageBody:"test"
isHTML:NO];
UIImage *image = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
NSData *data = UIImageJPEGRepresentation(image, 1);
[mailVC addAttachmentData: data
mimeType:#"image/jpeg"
fileName:imageName];
[self presentViewController:mailVC
animated:YES
completion:nil];
}
else {
}
}
Any help will be appreciate. Thanks.
I am using PassSlot which creates a Pass on the fly that can be added to passbook. I am trying to get it downloaded to the device to allow attaching to an email. Here is what I have so far:
[PassSlot passFromTemplateWithName:#"LoveCouponCards" withValues:values pass:^(PSPass *pass) {
[PassSlot downloadPass:pass pass:^(PSPass *pass) {
PKPass *pkpass = [pass performSelector:#selector(pass)];
NSLog(#"Pass: %#", pkpass);
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject:#"friend#example.com"];
[picker setToRecipients:toRecipients];
[picker addAttachmentData:pkpass mimeType:#"application/vnd.apple.pkpass" fileName:#"HI"];
// Fill out the email body text
NSString *emailBody = \\
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
}];
}];
The issue is that in the addAttachment part for the email, it throws an error that NSData can't relate to PKPass basically. How can I get pass converted to NSData so I can attach it?
UPDATE:
I tried doing
NSURL *url = pkpass.passURL;
NSData *so = [NSData dataWithContentsOfURL:url];
and then putting 'so' as the addAttachment, but it attached nothing to the email.
Firstly, the passURL property of PKPass doesn't quite work the way you think. It is not a URL to the pass itself. It is a URL that opens up the Passbook app and loads up that requested pass.
You can create a PKPass with NSData, but you can't reverse that process. It sounds as if you are trying to get a pass on device, and then e-mail it. That's not allowed - if it was, people could easily copy and distribute passes around (which isn't necessarily a good thing).
If you want to e-mail a user a pass you need to do it server, rather than client side. I'm afraid that what you're trying to do isn't possible using PassKit. Sorry!
Unfortunately the PassKit library does not provide a way to get back the NSData from a PKPass.
We already provide an API call that allows you to get the raw data of a pass.
We will extend our PassSlot SDK with a method that allows you to get the NSData without having the manually call this API method.
Update
The new SDK version 0.5 is now released. You can attach the pass with the following code:
[PassSlot passFromTemplateWithName:#"LoveCouponCards" withValues:values pass:^(PSPass *pass) {
[PassSlot downloadPass:pass pass:^(PSPass *pass) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:#[#"friend#example.com"]];
[picker addAttachmentData:pass.data mimeType:#"application/vnd.apple.pkpass" fileName:#"LoveCouponCard.pkpass"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:nil];
}];
}];
I wanted to show a gif so what i did was that i split my gif and showed it in a animation for UIImageView using this link.
http://iphonenativeapp.blogspot.com/2011/03/how-to-show-animation-in-iphoneipad-app.html
Now, i want to make the user copy that gif and paste it in the mail app.
If i used the array which contained all the split images of gif then 4-5 images get pasted in the mail app.
Please help me paste the gif. Thanks!
Gonna copy/paste my own answer from a similar question.
NSString *gifPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"volleyball.gif"];
NSData *gifData = [[NSData alloc] initWithContentsOfFile:gifPath];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setData:gifData forPasteboardType:#"com.compuserve.gif"];
[gifData release];
Edit just noticed you asked these 2 similar questions yourself.
Although you can use HTML based email -- for example:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *emailBody = #"<p><b>Hello World</b></p>";
[picker setMessageBody:emailBody isHTML:YES];
You can't insert inline images as you would typically in HTML. Inline images in HTML email use separate MIME parts that are referenced via a content-id element from the body of the message. MFMailComposeViewController doesn't give you control over the MIME structure of the message and thus doesn't let you add inline referenced content parts.
Embedding image data into <img> tags as base64 will sometimes work -- it depends on the email client and browser used to render it -- but it's not broadly portable.
FWIW, animated gifs appear to work with email in the new share sheets in iOS 6, which would automatically populate the gif in an email if the user selects mail:
NSString *gifPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"volleyball.gif"];
NSData *gifData = [[NSData alloc] initWithContentsOfFile:gifPath];
NSArray *activityItems = [NSArray arrayWithObjects:#"Here is an awesome body for the email.",gifData,nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityController.completionHandler = ^(NSString *activityType, BOOL completed){
// item was shared!
// you can check if it was email (or another type, like facebook or twitter) in the *activityType.
// completed is YES if they actually shared it, if they canceled, completed will be NO.
};
[navigationController presentViewController:activityController animated:YES completion:nil];
As iOS does not support the animated GIF format, I don't think it is possible to copy/paste the gif in the mail app. However, you can try attaching the gif file (not the split images) & composing a new email using MFMailComposeViewController. If you open the attachment on a non-iOS device, you should be able to see the animated GIF.
HTH,
Akshay