Fighting with images in E-mail - ios

My App sends an e-mail with images but there is no images in the e-mail.
I create a HTML string and send that as the body in the e-mail. But there is nothing in the e-mail when doing it on the device.
In the simulator is okay.
I put a WebView in my App and put the HTML in and its shows all the images.
Anyone know what could be the issue?

This is code I used to attach image in mail body
NSMutableString *htmlBody = [[NSMutableString alloc] initWithString:#"<html><body>"];
[htmlBody appendString:#"<p>Enter Text Here</p>"];
UIImage *emailImage = [UIImage imageNamed:#"image.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)];
NSString *base64String = [imageData base64EncodedString];
[htmlBody appendString:[NSString stringWithFormat:#"<p><b><img src='data:image/png;base64,%#'> </b></p>",base64String]];
[htmlBody appendString:#"</body></html>"];
NSLog(#"%#",htmlBody);
[mailComposer htmlBody isHTML:YES];
Hope it helps you..

Related

Save and load image from SQL database

I'm trying to display an image from my Database.
This part works nicely.
I save the image in the DB like this :
UIImage *resizedImg = [Generics scaleImage:self.photo.image toResolution:self.photo.frame.size.width and:self.photo.frame.size.height];
NSData *imgData = UIImageJPEGRepresentation(resizedImg, 0.9);
NSString *stringDataImage = [NSString stringWithFormat:#"%#",imgData];
[dict setObject:stringDataImage for key#"image"];
// POST Request to save the image in DB ...
Now when I try to load the image and set it into my UIImageView I do this way :
NSData *data = [[[MyUser sharedUser] picture] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
Or
NSData *data = [[[MyUser sharedUser] picture] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
self.imageView.image = [UIImage withData:data];
But this doesn't work.
data is not equal to imgData, I think it's the encoding but I can find the good one.
And [UIImage withData:data] return nil;
Can you help me?
EDIT :
Convert and save
UIImage *resizedImg = [Generics scaleImage:self.photo.image toResolution:self.photo.frame.size.width and:self.photo.frame.size.height];
NSData *imgData = UIImageJPEGRepresentation(resizedImg, 0.9);
[dict setObject:[imgData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength] forKey:#"image"];
Load the image
NSData *data = [[NSData alloc] initWithBase64EncodedData:[[MyUser sharedUser] picture] options:kNilOptions];
NSLog(#"%#", data);
self.image.image = [UIImage imageWithData:data];
You are converting the NSData to a string and saving that to your database. Two issues:
Your choice of using the stringWithFormat construct is an inefficient string representation of your data (resulting in string representation that is roughly twice the size). You probably want to use base64 (for which the string representation is only 33% larger).
You are saving your string representation, but never converting it back to binary format after retrieving it. You could write a routine to do this, but it's better to just use established base64 formats.
If you want to save the image as a string in your database, you should use base64. Historically we would have directed you to one of the many third party libraries (see How do I do base64 encoding on iphone-sdk?) for converting from binary data to base64 string (and back), or, iOS 7 now has native base 64 encoding (and exposes the private iOS 4 method, in case you need to support earlier versions of iOS).
Thus to convert NSData to NSString base64 representation:
NSString *string;
if ([data respondsToSelector:#selector(base64EncodedStringWithOptions:)]) {
string = [data base64EncodedStringWithOptions:kNilOptions]; // iOS 7+
} else {
string = [data base64Encoding]; // pre iOS7
}
And to convert base64 string back to NSData:
NSData *data;
if ([NSData instancesRespondToSelector:#selector(initWithBase64EncodedString:options:)]) {
data = [[NSData alloc] initWithBase64EncodedString:string options:kNilOptions]; // iOS 7+
} else {
data = [[NSData alloc] initWithBase64Encoding:string]; // pre iOS7
}
If you really want to turn binary data into a string you should be using base64 encoding. Luckily for you, NSData now supports Base64 natively
So you could get your string from data with:
NSString *stringDataImage = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
And you could turn this back into NSData with:
NSData *imgData = [[NSData alloc] initWithBase64EncodedString:stringDataImage options:kNilOptions];

Send a UIImage through an email

Given that I have a UIImage that is stored in a local variable:
UIImage *myImage = (Some image extracted somewhere)
How do I add this image as an attachment through MFMailComposeViewController ?
The image isn't stored on the users device and it's simply available at runtime.
So I can't access this image through a file name as other questions/examples have demonstrated.
Thank you!
NSData *imageData = UIImageJPEGRepresentation(photo, 0.9);
NSString *attachmentName = #"Any name.jpg"
[mailer addAttachmentData:imageData mimeType:#"image/jpeg" fileName:attachmentName];

Post image from local bundle to PinInterest iphone.

I have implemented the PinInterest through web view using this code:
NSString *description = #"Post your description here";
NSURL* sUrl = [NSString stringWithFormat:#"http://4.bp.blogspot.com/-w4oTZjlpgwo/T5_pi-KJPuI/AAAAAAAAAoM/rKm3E0XCbgY/s1600/red_rose_flower3.jpg"];// pass your link here with your image name
// NSLog(#"URL:%#", sUrl);
NSString *protectedUrl = ( NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,( CFStringRef)sUrl, NULL, (CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
NSLog(#"Protected URL:%#", protectedUrl);
NSString *imageUrl = [NSString stringWithFormat:#"\"%#\"", sUrl];
imageUrl=[[NSBundle mainBundle]pathForResource:mAppDelegate.imagename ofType:nil];
NSString *buttonUrl = [NSString stringWithFormat:#"\"http://pinterest.com/pin/create/button/?url=www.flor.com&media=%#&description=%#\"", protectedUrl, description];
NSMutableString *htmlString = [[NSMutableString alloc] initWithCapacity:1000];
[htmlString appendFormat:#"<html> <body>"];
[htmlString appendFormat:#"<p align=\"center\"><img border=\"0\" src=\"http://assets.pinterest.com/images/PinExt.png\" title=\"Pin It\" /></p>", buttonUrl];
[htmlString appendFormat:#"<p align=\"center\"><img width=\"300px\" height = \"400px\" src=%#></img></p>", imageUrl];
[htmlString appendFormat:#"<script type=\"text/javascript\" src=\"//assets.pinterest.com/js/pinit.js\"></script>"];
[htmlString appendFormat:#"</body> </html>"];
return htmlString;
And I have also tried using PinInterest iOS SDK, but all I found is that can only pin the image from url.
And I need to pin image from local bundle image. Any help will be greatly appreciated.
Read the document once, they said Right now, "we only support pinning an image from a url. In the future, we’ll add support for pinning local images." i think this answers your question. for more clarification reed this. hope this helps :)

MFMailComposeViewController few attachments

I'm successfully sending an email with attached UIImage from my app, but is it possible to attach more than one image with MFMailComposeViewController ?
you can save all the images in an NSMutable array and run the code
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#""];
for the count of your NSMutableArray. It will add the images to attachment field
for (int i = 0; i < [_textField0.emojiArray count]; i++)
{
emoji = [_textField0.emojiArray objectAtIndex:i];
UIImage *image = [EmojiResizer resizeImage2:[UIImage imageNamed:emoji.screenfilename]
imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
[mailViewController addAttachmentData:imageData mimeType:#"image/png" fileName:emoji.filename];
}
Do something like this.
Call addAttachment... for each attachment you wish to add.

How to resize the frame of embeded image in mail composer using MFMailComposeViewController in iphone

I have embeded a user image in message body of MFMailComposeSheet
in my iPhone app.
NSMutableString *messageBody = [[[NSMutableString alloc] initWithString:#"<html><body>"] retain];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(capturedProfileImage)];
NSString *base64String = [self base64forData:imageData];
[messageBody appendFormat:#"<p> %#.</p>",textMessage];
[messageBody appendFormat:#"<p>REGARDS</p>"];
[messageBody appendFormat:#"<p><b><img src='data:image/png;base64,%#'> </b></p>", base64String];
[messageBody appendFormat:#"<p> %#.</p>",userName];
[messageBody appendFormat:#"<p> %#.</p>",userPhone];
[messageBody appendFormat:#"</body></html>"];
Require format is like as follows:
But the embedded image displayed in its actual size, and I need to display in 57x57 pixels. How do I fix the width and height of the image as 57x57?
Fixed:
[messageBody appendFormat:#"<p><b><img src='data:image/png;base64 , %#' style='width:57px; height=57px;'> </b></p>", base64String];

Resources