<?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>iOS Developer Tips Blog &#187; Camera</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/camera/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:40:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Writing a Flashlight Application using the iPhone LED</title>
		<link>http://iPhoneDeveloperTips.com/camera/flashlight-application-using-the-iphone-led.html</link>
		<comments>http://iPhoneDeveloperTips.com/camera/flashlight-application-using-the-iphone-led.html#comments</comments>
		<pubDate>Fri, 17 Dec 2010 03:43:25 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Camera]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=7118</guid>
		<description><![CDATA[With the introduction of the iPhone 4, Apple has added an LED alongside the camera. It&#8217;s amazing how bright this little bugger is, ideal for a flashlight! Since the release of the iPhone 4 I&#8217;ve been meaning to take a few minutes and write a flashlight application using the LED. My understanding from what I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>With the introduction of the iPhone 4, Apple has added an LED alongside the camera. It&#8217;s amazing how bright this little bugger is, ideal for a flashlight!</p>
<p>Since the release of the iPhone 4 I&#8217;ve been meaning to take a few minutes and write a flashlight application using the LED. My understanding from what I&#8217;ve gleaned from the API&#8217;s is that in order to turn on the LED, we need to essentially go through the same steps we would to capture video &#8211; if you know of a quicker and/or simpler process, please post some code below.<br />
<span id="more-7118"></span></p>
<h5>iPhone Flashlight UI</h5>
<p>The user interface of this app is about as simple as they get &#8211; nothing more than a button to toggle the flashlight on and off.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/12/flashlight.png" alt="" width="404" height="300" /></p>
<p>The code for the UI is equally as trivial, in <strong>loadView</strong>, check if the device has a torch and if so, add a button to toggle the flashlight state.</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>loadView
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self setView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  AVCaptureDevice <span style="color: #002200;">*</span>device <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>AVCaptureDevice defaultDeviceWithMediaType<span style="color: #002200;">:</span>AVMediaTypeVideo<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// If torch supported, add button to toggle flashlight on/off</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>device hasTorch<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    flashlightButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">120</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">98</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>flashlightButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TorchOn.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
     <span style="color: #002200;">&#91;</span>flashlightButton addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span> UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;      
&nbsp;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>flashlightButton<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Toggling the Flashlight On and Off</h5>
<p>The event management code for this app is nothing more than swapping the button background image to reflect the current state of the flashlight followed by a call to the code that will toggle the LED on and off.</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: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> flashlightButton<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>flashlightOn <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      flashlightOn <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
      <span style="color: #002200;">&#91;</span>flashlightButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TorchOff.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span>
    <span style="color: #002200;">&#123;</span>
      flashlightOn <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
      <span style="color: #002200;">&#91;</span>flashlightButton setBackgroundImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TorchOn.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>self toggleFlashlight<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>AVCaptureSession Configuration</h5>
<p>There are a few objects that we&#8217;ll need to interact with to get everything working. <strong>AVCaptureDevice</strong> is the object we&#8217;ll use to represent the physical video device. An <strong>AVCaptureSession</strong> object will manage the flow of data. And finally, <strong>AVCaptureDeviceInput</strong> will represent the video data.</p>
<p>With the objects above defined, we configure the AV session to set the torch on and start the session running&#8230;</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>toggleFlashlight
<span style="color: #002200;">&#123;</span>
  AVCaptureDevice <span style="color: #002200;">*</span>device <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>AVCaptureDevice defaultDeviceWithMediaType<span style="color: #002200;">:</span>AVMediaTypeVideo<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>device.torchMode <span style="color: #002200;">==</span> AVCaptureTorchModeOff<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Create an AV session</span>
    AVCaptureSession <span style="color: #002200;">*</span>session <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AVCaptureSession alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create device input and add to current session</span>
    AVCaptureDeviceInput <span style="color: #002200;">*</span>input <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>AVCaptureDeviceInput deviceInputWithDevice<span style="color: #002200;">:</span>device error<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>session addInput<span style="color: #002200;">:</span>input<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create video output and add to current session</span>
    AVCaptureVideoDataOutput <span style="color: #002200;">*</span>output <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AVCaptureVideoDataOutput alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>session addOutput<span style="color: #002200;">:</span>output<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Start session configuration</span>
    <span style="color: #002200;">&#91;</span>session beginConfiguration<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>device lockForConfiguration<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Set torch to on</span>
    <span style="color: #002200;">&#91;</span>device setTorchMode<span style="color: #002200;">:</span>AVCaptureTorchModeOn<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>device unlockForConfiguration<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>session commitConfiguration<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Start the session</span>
    <span style="color: #002200;">&#91;</span>session startRunning<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Keep the session around</span>
    <span style="color: #002200;">&#91;</span>self setAVSession<span style="color: #002200;">:</span>session<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>output release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">else</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>AVSession stopRunning<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>AVSession release<span style="color: #002200;">&#93;</span>, AVSession <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Caveats</h5>
<p>Stating the obvious, this application will only work on iPhone 4+ devices. Also, this application works on only on a device &#8211; <span style="color: #993300;">you will receive compile time errors regarding <strong>AVCaptureSession</strong> if you attempt to build the app for the simulator.</span></p>
<p>One last thought &#8211; chances are running the LED for any length of time may give your battery a pretty intense workout. It may be worthwhile to keep an eye on your battery indicator if you plan to turn the light on for more than a few minutes.</p>
<h5>iPhone LED Flashlight- Xcode Project</h5>
<p>Here is the link to <a href="http://iOSDeveloperTips.com/wp-content/uploads/2010/12/flashlightApp.zip">download the source code</a> for the iPhone LED Flashlight application.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/camera/flashlight-application-using-the-iphone-led.html/feed</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Camera Application to Take Pictures and Save Images to Photo Album</title>
		<link>http://iPhoneDeveloperTips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html</link>
		<comments>http://iPhoneDeveloperTips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html#comments</comments>
		<pubDate>Fri, 26 Feb 2010 14:56:59 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Camera]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5553</guid>
		<description><![CDATA[Editor&#8217;s Note:If you would like to save the UIImage from the camera to a PNG of JPEG file versus the Photo Album, you can find an example here: Save UIImage Object as a PNG or JPEG File. Also, to email an image from the camera, take a look at this example: How to Send Email [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>Editor&#8217;s Note:</strong>If you would like to save the UIImage from the camera to a PNG of JPEG file versus the Photo Album, you can find an example here: <a href="http://iphonedevelopertips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html">Save UIImage Object as a PNG or JPEG File</a>. Also, to email an image from the camera, take a look at this example: <a href="http://iphonedevelopertips.com/email/how-to-send-email-with-attachments-example-using-iphone-camera-to-email-a-photo.html">How to Send Email with Attachments – Example Using iPhone Camera to Email a Photo</a></em></p>
<p>It&#8217;s surprising easy to create a bare bones camera application on the iPhone. <strong>UIImagePickerController</strong> provides a means to access the camera, take a photo and preview the results. There is also an option to allow resizing and scaling of a photo once captured. Using <strong>UIImageWriteToSavedPhotosAlbum</strong> in the UIKit, you can easily save an image to the Photo Album.</p>
<p>The image on the left in the figure below shows the camera active in the application. The image on the right is the preview option once a photo has been taken.<br />
<span id="more-5553"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/02/photo12.png" /></p>
<h5>Start the Camera</h5>
<p>To work with the camera we begin by creating a <strong>UIImagePickerController</strong> object and setting the <strong>sourceType</strong> to <strong>UIImagePickerControllerSourceTypeCamera</strong>. For this example, I set <strong>allowsImageEditing</strong> to NO to disable editing of photos image. I use presentModalViewController to initiate the display of the camera.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><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>;</pre></div></div>

<h5>Save Image to Photo Album</h5>
<p>Once a photo has been taken, the method <strong>didFinishPickingMediaWithInfo</strong> will be called, providing the opportunity to write the image to the Photo Album:</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;">// Save image</span>
  UIImageWriteToSavedPhotosAlbum<span style="color: #002200;">&#40;</span>image, self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>image<span style="color: #002200;">:</span>didFinishSavingWithError<span style="color: #002200;">:</span>contextInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>picker release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Notice the call selector reference above, this selector will be called once the image has been written to the system. For this example I display an alert showing the result of attempting to save 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>image<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>image didFinishSavingWithError<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 contextInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contextInfo
<span style="color: #002200;">&#123;</span>
  UIAlertView <span style="color: #002200;">*</span>alert;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Unable to save the image  </span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>error<span style="color: #002200;">&#41;</span>
    alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error&quot;</span> 
                            message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to save image to Photo Album.&quot;</span> 
                            delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span> 
                            otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// All is well</span>
    alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Success&quot;</span> 
                            message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Image saved to Photo Album.&quot;</span> 
                            delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span> 
                            otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Optional Updates</h5>
<p>This is a good, if not simple, place to start. Here are two additional updates to this application you can tinker with:</p>
<p>#1 &#8211; Check for a camera on device. Refer to this post <a  target="_blank"  href="http://iphonedevelopertips.com/camera/iphone-support-camera-and-recording-video.html">Does iPhone Support Camera</a> to learn more.</p>
<p>#2 &#8211; Set the image editing option to YES. This will require a few additional changes including a request to the information dictionary to return the cropping rectangle that was applied to the original image (see <strong>UIImagePickerControllerCropRect</strong>).</p>
<h5>Source Code</h5>
<p>You can download the source code for the <a href="http://iphonedevelopertips.com/wp-content/uploads/2010/02/camera.zip">camera application here</a>.</p>
<h5>Simulator Not Supported</h5>
<p>One last note, this application will only run on an actual device as the simulator does not have camera support.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html/feed</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Does iPhone Support Camera and Recording of Video?</title>
		<link>http://iPhoneDeveloperTips.com/camera/iphone-support-camera-and-recording-video.html</link>
		<comments>http://iPhoneDeveloperTips.com/camera/iphone-support-camera-and-recording-video.html#comments</comments>
		<pubDate>Mon, 23 Nov 2009 14:42:31 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Camera]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4552</guid>
		<description><![CDATA[This is the final tip in a series of four that is based on content from the O&#8217;Reilly book Head First iPhone Development. O’Reilly and iPhone Developer Tips are collaborating to give away a free ebook (electronic copy) of Head First iPhone Development each Friday in November. You can register in this weeks drawing by [...]]]></description>
			<content:encoded><![CDATA[<p>This is the final tip in a series of four that is based on content from the O&#8217;Reilly book <a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12">Head First iPhone Development</a>. O’Reilly and iPhone Developer Tips are collaborating to give away a free ebook (electronic copy) of Head First iPhone Development each Friday in November. You can register in this weeks drawing by referring a friend to this tip – see the Refer-a-Friend button near the bottom of this post.</p>
<p>This tip is from Chapter 9 &#8211; Camera, Mapkit and Core Location and shows how to make a quick check on the status/availability of the camera on device.<br />
<span id="more-4552"></span></p>
<h5>Book Giveaway</h5>
<p>We are no longer accepting entries for today&#8217;s giveaway.</p>
<h5>Is the Camera Available and Ready?</h5>
<p>Not all devices support the same source media capabilities, for example, there is no currently no camera on the iPod touch. In addition, even if a device does support a specific media source, that source could be busy &#8211; for example, the camera on an iPhone may be in use.</p>
<p>This short snippet of code demonstrates how to check if a device has a camera and/or if the camera is available:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>UIImagePickerController isSourceTypeAvailable<span style="color: #002200;">:</span>UIImagePickerControllerSourceTypeCamera<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Camera is available and ready&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Camera is not available&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Note that when using the Simulator within Xcode, this call will return NO, as there is no camera available.</p>
<h5>Does iPhone Support Recording Video?</h5>
<p>The only iPhone to support video recording is the 3GS. There are two checks required before you can use the technology in your application. First, check to see if a camera is available, and second, verify the media types available on the device includes movies.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>UIImagePickerController isSourceTypeAvailable<span style="color: #002200;">:</span>UIImagePickerControllerSourceTypeCamera<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Camera is available and ready&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Camera is not available&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>media <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImagePickerController
     availableMediaTypesForSourceType<span style="color: #002200;">:</span> UIImagePickerControllerSourceTypeCamera<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>media containsObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;public.movie&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Video recording available&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">else</span> 
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Video recording is not available&quot;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/camera/iphone-support-camera-and-recording-video.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Save an Image to Camera Roll</title>
		<link>http://iPhoneDeveloperTips.com/camera/save-an-image-to-camera-roll.html</link>
		<comments>http://iPhoneDeveloperTips.com/camera/save-an-image-to-camera-roll.html#comments</comments>
		<pubDate>Thu, 07 May 2009 07:08:02 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Camera]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=2257</guid>
		<description><![CDATA[Saving an image to the camera roll on the iPhone is as close as a method call in the UIKit. However, it takes a few steps to wrap together code to manage error handling and notification that the image has been saved. Let&#8217;s see how this works. Let&#8217;s begin with the method description to save [...]]]></description>
			<content:encoded><![CDATA[<p>Saving an image to the camera roll on the iPhone is as close as a method call in the UIKit. However, it takes a few steps to wrap together code to manage error handling and notification that the image has been saved.  Let&#8217;s see how this works.</p>
<p>Let&#8217;s begin with the method description to save an image:<br />
<span id="more-2257"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #a61390;">void</span> UIImageWriteToSavedPhotosAlbum<span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span>image, 
                  <span style="color: #a61390;">id</span> completionTarget, <span style="color: #a61390;">SEL</span> completionSelector, <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>contextInfo<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><strong><em>completionTarget</em></strong> refers to the object in which the <strong><em>completionSelector</em></strong> can be found. In other words, what object has the method that will be called once the file write is complete? <strong><em>contextInfo</em></strong> is a <em>void *</em> that you can use to specify content to be passed to the <strong><em>completionSelector</em></strong>.</p>
<p>Obviously, the image is a required paramater. The other three are only needed if you prefer to be notified asynchronously when the write is complete. Here&#8217;s how a call to the above method might look:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">  <span style="color: #11740a; font-style: italic;">// Image to save</span>
  UIImage <span style="color: #002200;">*</span>img <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ImageName.png&quot;</span><span style="color: #002200;">&#93;</span>;  
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Request to save the image to camera roll</span>
  UIImageWriteToSavedPhotosAlbum<span style="color: #002200;">&#40;</span>img, self, 
              <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>image<span style="color: #002200;">:</span>didFinishSavingWithError<span style="color: #002200;">:</span>contextInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Here we are specifying that the selector to be called upon completion is within this object (self). A template for this selector could look as follows:</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>image<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>image didFinishSavingWithError<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 
             contextInfo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>contextInfo
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Was there an error?</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>error <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Show error message...</span>
&nbsp;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span>  <span style="color: #11740a; font-style: italic;">// No errors</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Show message image successfully saved</span>
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span></pre></div></div>

<p>Add code for displaying the appropriate message based on success or failure of the image save, and you&#8217;re good to go.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/camera/save-an-image-to-camera-roll.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Capture a Screenshot on the iPhone</title>
		<link>http://iPhoneDeveloperTips.com/camera/capture-a-screenshot-on-iphone.html</link>
		<comments>http://iPhoneDeveloperTips.com/camera/capture-a-screenshot-on-iphone.html#comments</comments>
		<pubDate>Thu, 05 Feb 2009 03:43:17 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Camera]]></category>
		<category><![CDATA[screenshot]]></category>
		<category><![CDATA[simulator]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1493</guid>
		<description><![CDATA[If you ever have a need to create a screenshot from an iPhone (not the simulator), or if you are unsure why you would ever want to do this, read on&#8230; For the most part, capturing images of your application is as simple as running the simulator and pressing Shift-Command-4. However, when working with the [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever have a need to create a screenshot from an iPhone (not the simulator), or if you are unsure why you would ever want to do this, read on&#8230;</p>
<p>For the most part, capturing images of your application is as simple as running the simulator and pressing Shift-Command-4. However, when working with the simulator there are times when you cannot get to the screen contents you want to capture or otherwise are constrained by the simulator.<br />
<span id="more-1493"></span></p>
<p>For instance, an application I was working on recently involved taking a picture with the camera. Once the photo was taken, the user was to provide some additional information about the photo. Because the next steps were non-trivial, we decided a help screen was in order. To create this screen, I wanted a screenshot of a photo taken by the application, which I would then markup using an image editing program to write notes as to what the user needed to do next. </p>
<p>Problem is, there are limitations of the camera when working with the simulator.  The end result was, when running in the simulator the screen I needed to explain could not be shown with an actual photograph taken from the application (it would always be a black screen).</p>
<p>The work around that I came up with was to run the application on a real device, get to the screen I wanted to capture, create a screenshot, email myself the image, then edit as needed and incorporate into the application as a help screen.</p>
<p>The steps for capturing a screenshot on an iPhone are to navigate to the screen you want to capture, then:</p>
<p>Step #1 &#8211; Press and hold the Home control<br />
Step #2 &#8211; Press the Sleep control</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/08/screenshot.png" /></p>
<p>The captured image will be stored in the Camera Roll.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/camera/capture-a-screenshot-on-iphone.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

