<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>[iPhoneiOS dev:tips]; &#187; Email</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/email/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Wed, 08 Sep 2010 17:39:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to Send Email with Attachments &#8211; Example Using iPhone Camera to Email a Photo</title>
		<link>http://iPhoneDeveloperTips.com/email/how-to-send-email-with-attachments-example-using-iphone-camera-to-email-a-photo.html</link>
		<comments>http://iPhoneDeveloperTips.com/email/how-to-send-email-with-attachments-example-using-iphone-camera-to-email-a-photo.html#comments</comments>
		<pubDate>Wed, 10 Mar 2010 08:03:12 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Email]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5772</guid>
		<description><![CDATA[In this post: Camera Application to Take Pictures and Save Images to Photo Album, I demonstrated how you can take photos with the iPhone camera and save the captured images to the Photo Album. A reader asked if it would be possible to email the camera image in place of writing to the Photo Album, [...]]]></description>
			<content:encoded><![CDATA[<p>In this post: <a href="http://iphonedevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html">Camera Application to Take Pictures and Save Images to Photo Album</a>, I demonstrated how you can take photos with the iPhone camera and save the captured images to the Photo Album. A reader asked if it would be possible to email the camera image in place of writing to the Photo Album, which is the focus of this tip. </p>
<p>Building on the previous post, the example created here starts the camera on the iPhone, and once a photo is snapped, launches the email application, attaching the resulting image to the email.</p>
<h5>Start the iPhone Camera</h5>
<p>The user interface of this application is quite simple, there is one button on the UI that will start the camera. Once the button is tapped, the method shown below will be called. Here we create an image picker controller, set the source type to the camera, point the delegate to self and specify not to allow image editing. From there, simply show a modal view controller to enable the camera.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create image picker controller</span>
  UIImagePickerController <span style="color: #002200;">*</span>imagePicker <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImagePickerController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set source to the camera</span>
  imagePicker.sourceType <span style="color: #002200;">=</span>  UIImagePickerControllerSourceTypeCamera;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Delegate is self</span>
  imagePicker.delegate <span style="color: #002200;">=</span> self;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Allow editing of image ?</span>
  imagePicker.allowsImageEditing <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Show image picker</span>
  <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>imagePicker animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;	
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Convert Camera Image to NSData Object</h5>
<p>Once the camera has taken a photo, the method below will be called, passing in a dictionary of related information, such as the original image, edited image (if any), URL to a movie (when applicable), etc. We grab the image from the dictionary and create a <strong>UIImage</strong> object. From there, dismiss the camera, then pass the camera image to the method <strong>emailImage</strong> where we will construct an email and append the image.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">--</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> imagePickerController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImagePickerController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>picker didFinishPickingMediaWithInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>info
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Access the uncropped image from info dictionary</span>
  UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>info objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;UIImagePickerControllerOriginalImage&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Dismiss the camera</span>
  <span style="color: #002200;">&#91;</span>self dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Pass the image from camera to method that will email the same</span>
  <span style="color: #11740a; font-style: italic;">// A delay is needed so camera view can be dismissed</span>
  <span style="color: #002200;">&#91;</span>self performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>emailImage<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>image afterDelay<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Release picker</span>
  <span style="color: #002200;">&#91;</span>picker release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Email Camera Image</h5>
<p>The last step is to compose an email and attach the image from the camera. We begin be creating a <strong>MFMailComposeViewController</strong> object, setting the delegate to self. We then step through the configuration preferences for the email, setting the subject, the list of recipients (to, carbon copy, and blind carbon copy) and creating the body of the message.</p>
<p>Our next step is to convert the<strong>UIImage</strong> from the camera into an NSData object, which we can attach to our email. As you&#8217;ll notice in the code below, I&#8217;ve created the <strong>NSData</strong> as a PNG representation, you could also use JPG format if you prefer (which allows you to specify a value for image compression). </p>
<p>Once the image is attached to the email, we show a modal view controller which will launch the email application &#8211; one note, I am making the assumption that iPhone OS 3.x is the active platform.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>emailImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>image
<span style="color: #002200;">&#123;</span>
  MFMailComposeViewController <span style="color: #002200;">*</span>picker <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MFMailComposeViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  picker.mailComposeDelegate <span style="color: #002200;">=</span> self;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set the subject of email</span>
  <span style="color: #002200;">&#91;</span>picker setSubject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Picture from my iPhone!&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Add email addresses</span>
  <span style="color: #11740a; font-style: italic;">// Notice three sections: &quot;to&quot; &quot;cc&quot; and &quot;bcc&quot;	</span>
  <span style="color: #002200;">&#91;</span>picker setToRecipients<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;emailaddress1@domainName.com&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;emailaddress2@domainName.com&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>picker setCcRecipients<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;emailaddress3@domainName.com&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;	
  <span style="color: #002200;">&#91;</span>picker setBccRecipients<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;emailaddress4@domainName.com&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Fill out the email body text</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>emailBody <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;I just took this picture, check it out.&quot;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// This is not an HTML formatted email</span>
  <span style="color: #002200;">&#91;</span>picker setMessageBody<span style="color: #002200;">:</span>emailBody isHTML<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create NSData object as PNG image data from camera image</span>
  <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> UIImagePNGRepresentation<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Attach image data to the email</span>
  <span style="color: #11740a; font-style: italic;">// 'CameraImage.png' is the file name that will be attached to the email</span>
  <span style="color: #002200;">&#91;</span>picker addAttachmentData<span style="color: #002200;">:</span>data mimeType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;image/png&quot;</span> fileName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CameraImage&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Show email view	</span>
  <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>picker animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Release picker</span>
  <span style="color: #002200;">&#91;</span>picker release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>mailComposeController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MFMailComposeViewController<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>controller didFinishWithResult<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MFMailComposeResult<span style="color: #002200;">&#41;</span>result error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Called once the email is sent</span>
  <span style="color: #11740a; font-style: italic;">// Remove the email view controller	</span>
  <span style="color: #002200;">&#91;</span>self dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Caveat</h5>
<p>To keep things simple, this application runs on a single thread. It works fine, however, there is a few second delay when moving from the camera application to the email application. Ideally you would add a little logic to better manage the user interface.</p>
<h5>Source Code</h5>
<p>Here is the link to the source code: <a href="http://iphonedevelopertips.com/wp-content/uploads/2010/03/cameraEmail.zip">Email Camera Image Application</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/email/how-to-send-email-with-attachments-example-using-iphone-camera-to-email-a-photo.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
