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, which is the focus of this tip.
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.
Start the iPhone Camera
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.
Convert Camera Image to NSData Object
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 UIImage object. From there, dismiss the camera, then pass the camera image to the method emailImage where we will construct an email and append the image.
Email Camera Image
The last step is to compose an email and attach the image from the camera. We begin be creating a MFMailComposeViewController 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.
Our next step is to convert theUIImage from the camera into an NSData object, which we can attach to our email. As you’ll notice in the code below, I’ve created the NSData as a PNG representation, you could also use JPG format if you prefer (which allows you to specify a value for image compression).
Once the image is attached to the email, we show a modal view controller which will launch the email application – one note, I am making the assumption that iPhone OS 3.x is the active platform.
Caveat
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.
Source Code
Here is the link to the source code: Email Camera Image Application.
No comments:
Post a Comment