<?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>Mike Zupan&#039;s Random Blog&#187; Uncategorized</title>
	<atom:link href="http://zcentric.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://zcentric.com</link>
	<description>A blog about linux, programming and more</description>
	<lastBuildDate>Fri, 16 Mar 2012 23:24:15 +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>Post a UIImage to the web</title>
		<link>http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/</link>
		<comments>http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 15:18:53 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[UIImagePickerController]]></category>

		<guid isPermaLink="false">http://iphone.zcentric.com/?p=218</guid>
		<description><![CDATA[So here is something a lot of people have been wondering to do in the forums. How do I take a UIImage or any image and post it to a website. So this will go over how to do that. There are two ways to tackle this issue. One we can base64 encode the file [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fzcentric.com%2F2008%2F08%2F29%2Fpost-a-uiimage-to-the-web%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fzcentric.com%2F2008%2F08%2F29%2Fpost-a-uiimage-to-the-web%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>So here is something a lot of people have been wondering to do in the forums. How do I take a UIImage or any image and post it to a website. So this will go over how to do that.</p>
<p>There are two ways to tackle this issue. One we can base64 encode the file and post is normally inside a XML or JSON instance or we can simulate a normal HTML post. This tutorial will go over the HTML post.</p>
<p>This will start off where <a title="Permanent Link to Using a UIImagePickerController" rel="bookmark" href="../2008/08/28/using-a-uiimagepickercontroller/">Using a UIImagePickerController</a> left off. So you can grab the code and start there if you want. So lets begin.</p>
<p>So first open up <strong>testPickerViewController.h</strong> and we want to add in one outlet and one action.</p>
<p>So here is the outlet we want to add<br />
<code>IBOutlet UIButton *upload;</code><br />
Here is the action we want to add<br />
<code>IBOutlet UIButton *upload;</code><br />
Now double click on <strong>testPickerViewController.xib</strong> and we need to connect the new outlet and action. We also need to create our new upload button. So drag around the current items and place the new button under the grab button. Then you want to make the button hidden by default. The option is as shown below. Do get to that selector you do <strong>Tools -&gt; Attributes Inspector</strong></p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture1.jpg"><img class="alignnone size-medium wp-image-220" title="screen-capture1" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture1-221x300.jpg" alt="" width="221" height="300" /></a></p>
<p>You might also want to setup your UIImageView to aspect fit. If the image is larger then your box you created in IB it will shrink the image to fill it. Click on the UIImageView and in the Attributes Inspector select the following drop down for <strong>Mode</strong>. </p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-31.jpg"><img class="alignnone size-medium wp-image-221" title="screen-capture-31" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-31-300x178.jpg" alt="" width="300" height="178" /></a></p>
<p>Now you want to make your connections to the new outlet and action we created in code. So here is another screenshot of what they should look like</p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-21.jpg"><img class="alignnone size-medium wp-image-223" title="screen-capture-21" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-21-300x151.jpg" alt="" width="300" height="151" /></a></p>
<p>Now it is back to the code. So save this and you can quit IB. </p>
<p>So open up <strong>testPickerViewController.m</strong><em> </em>and find the <strong>imagePickerController</strong> method and at the end add <br />
<code>upload.hidden = NO;</code></p>
<p>That will show our upload button once a image is set. </p>
<p>So now we need to create our <strong>uploadImage</strong> method that gets called then the button is pressed. So it is below and hopefully pretty well commented. </p>
<pre><code>- (IBAction)uploadImage {
	/*
	 turning the image into a NSData object
	 getting the image back out of the UIImageView
	 setting the quality to 90
	*/
	NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
	// setting up the URL to post to
	NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";

	// setting up the request object now
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
	[request setURL:[NSURL URLWithString:urlString]];
	[request setHTTPMethod:@"POST"];

	/*
	 add some header info now
	 we always need a boundary when we post a file
	 also we need to set the content type

	 You might want to generate a random boundary.. this is just the same
	 as my output from wireshark on a valid html post
	*/
	NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

	/*
	 now lets create the body of the post
	*/
	NSMutableData *body = [NSMutableData data];
	[body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[NSData dataWithData:imageData]];
	[body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
	// setting the body of the post to the reqeust
	[request setHTTPBody:body];

	// now lets make the connection to the web
	NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
	NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

	NSLog(returnString);
}</code></pre>
<p>So now if you build and go you will upload the image you selected to the following image URL</p>
<p><a href="http://iphone.zcentric.com/uploads/ipodfile.jpg">http://iphone.zcentric.com/uploads/ipodfile.jpg</a></p>
<p>Below is my PHP file I am using to handle uploads. </p>
<pre><code>$uploaddir = './uploads/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "http://iphone.zcentric.com/uploads/{$file}";
}</code></pre>
<p>It is nothing special. I do an echo so you can see in the console what the file is. If you switch the filename=&#8221;" part in the uploadImage section it will be placed as another file.</p>
<p>Files will be removed once they are 10 minutes old on the server so they won&#8217;t last but you can use it as a playground. </p>
<p>As always here is my <a href="http://iphone.zcentric.com/files/testpicker2.zip">code</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/feed/</wfw:commentRss>
		<slash:comments>113</slash:comments>
		</item>
		<item>
		<title>Using a UIImagePickerController</title>
		<link>http://zcentric.com/2008/08/28/using-a-uiimagepickercontroller/</link>
		<comments>http://zcentric.com/2008/08/28/using-a-uiimagepickercontroller/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 17:54:57 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[UIImagePickerController]]></category>

		<guid isPermaLink="false">http://iphone.zcentric.com/?p=196</guid>
		<description><![CDATA[Apple allows you great access into the images you have taken with your camera or saved on the phone via Safari or such. It also allows you to load up the camera very easily in your code to take pictures from your application. So the first thing we want to do is start a new [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fzcentric.com%2F2008%2F08%2F28%2Fusing-a-uiimagepickercontroller%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fzcentric.com%2F2008%2F08%2F28%2Fusing-a-uiimagepickercontroller%2F&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Apple allows you great access into the images you have taken with your camera or saved on the phone via Safari or such. It also allows you to load up the camera very easily in your code to take pictures from your application. </p>
<p>So the first thing we want to do is start a new project and call it <strong>testPicker. </strong>You want to create a View based application.</p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture1.png"><img class="alignnone size-medium wp-image-202" title="screen-capture1" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture1-300x288.png" alt="" width="300" height="288" /></a></p>
<p>Now that our application is created we want to open up IB and create our view. So double click on the <strong>testPickerViewController.xib</strong> file and it will launch IB. The first thing you will notice is that is is a blank view. So lets grab a round rectangle button on the view and also a image view controller. So now our view should look something like this</p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-1.png"><img class="alignnone size-medium wp-image-203" title="screen-capture-1" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-1-206x300.png" alt="" width="206" height="300" /></a></p>
<p>So now we have to create 2 outlets and an action. The outlets are for the items we placed on the view and the action is for the button click. So make sure you are selecting the <strong>File&#8217;s Owner </strong>name and it should be of type testPickerViewController and create 2 outlets and an action. Name them like the following image. </p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-2.png"><img class="alignnone size-medium wp-image-204" title="screen-capture-2" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-2-300x157.png" alt="" width="300" height="157" /></a></p>
<p>Now we need to connect them. So you just drag the outlet to the correct item on the view and then grab the action over to the button and a box will popup and select touch up inside as an option. It should look like the following</p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture.jpg"><img class="alignnone size-medium wp-image-205" title="screen-capture" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-300x136.jpg" alt="" width="300" height="136" /></a></p>
<p>Now we have to write the new class file. Make sure you are still selected to <strong>File&#8217;s Owner</strong> and go to <strong>File -> Write Class Files</strong>. You should get the following if you did it the correct spot. </p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-1.jpg"><img class="alignnone size-medium wp-image-206" title="screen-capture-1" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-1-300x248.jpg" alt="" width="300" height="248" /></a></p>
<p>Save the file. It will ask you to merge or overwrite. For this just overwrite the files. </p>
<p>Now that we have our view setup, lets do some code. So the first thing is we must set the UIImagePickerController delegates.</p>
<p>So open up <strong>testPickerViewController.h</strong> and we want to add in the following to the class reference.<br />
<code>UINavigationControllerDelegate, UIImagePickerControllerDelegate></code><br />
Now we need to add the reference to our image picker so add the following<br />
<code>UIImagePickerController *imgPicker;</code><br />
Now we need a property for it.<br />
<code>@property (nonatomic, retain) UIImagePickerController *imgPicker;</code><br />
So the whole file looks something like this now</p>
<pre><code>@interface testPickerViewController : UIViewController  <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
    IBOutlet UIButton *button;
    IBOutlet UIImageView *image;
    UIImagePickerController *imgPicker;
}
- (IBAction)grabImage;

@property (nonatomic, retain) UIImagePickerController *imgPicker;

@end</code></pre>
<p>Now save the file and open up <strong>testPickerViewController.m</strong>. First we need to synthesize the image picker<br />
<code>@synthesize imgPicker;</code><br />
Now we need to create the <strong>viewDidLoad</strong> method and init the image picker.</p>
<pre><code>- (void)viewDidLoad {
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}</code></pre>
<p>There are 3 different types of sources you can use</p>
<ul>
<li>UIImagePickerControllerSourceTypeCamera : to load the camera</li>
<li>UIImagePickerControllerSourceTypePhotoLibrary : to load images from library</li>
<li>UIImagePickerControllerSourceTypeSavedPhotosAlbum : loads all images saved</li>
</ul>
<p>Also we set the editing to YES. This will allow your user to crop the image and such. The picker controller&#8217;s delegate function we will add later will return the information so you can edit the UIImage that is returned if you really wanted. We will not cover that here. At least not yet.</p>
<p>Now lets load the picker on the button click so make the <strong>grabImage</strong> method look like this</p>
<pre><code>- (IBAction)grabImage {
[self presentModalViewController:self.imgPicker animated:YES];
}</code></pre>
<p>Now if we click build and go, you will be able to click on the grab images button and your image picker will slide up from the bottom and allow you select a image.</p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-2.jpg"><img class="alignnone size-medium wp-image-201" title="screen-capture-2" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-2-161x300.jpg" alt="" width="161" height="300" /></a></p>
<p>Now we need to add in our delegate methods so we can handle the image when a user selects it. The only one we will really cover is <strong>imagePickerController</strong>. Here is my complete function to take the selected image and then place it into the imageview we created in IB.</p>
<pre><code>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}</code></pre>
<p>Here is our final product.</p>
<p><a href="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-3.jpg"><img class="alignnone size-medium wp-image-200" title="screen-capture-3" src="http://iphone.zcentric.com/wp-content/uploads/2008/08/screen-capture-3-161x300.jpg" alt="" width="161" height="300" /></a></p>
<p>As always you can grab the code <a href="http://iphone.zcentric.com/files/testpicker.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://zcentric.com/2008/08/28/using-a-uiimagepickercontroller/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
	</channel>
</rss>

