`

Post a UIImage to the web

阅读更多

http://iphone.zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

 

Post a UIImage to the web

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 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.

 

This will start off where Using a UIImagePickerController left off. So you can grab the code and start there if you want. So lets begin.

So first open up testPickerViewController.h and we want to add in one outlet and one action.

So here is the outlet we want to add

IBOutlet UIButton *upload;

Here is the action we want to add

IBOutlet UIButton *upload;

Now double click on testPickerViewController.xib 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 Tools -> Attributes Inspector

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 Mode

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

Now it is back to the code. So save this and you can quit IB. 

So open up testPickerViewController.m and find the imagePickerController method and at the end add 

upload.hidden = NO;

That will show our upload button once a image is set.

So now we need to create our uploadImage method that gets called then the button is pressed. So it is below and hopefully pretty well commented.

 

- (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:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	[body appendData:[NSData dataWithData:imageData]];
	[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",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);
}
 

 

 

 

So now if you build and go you will upload the image you selected to the following image URL

http://iphone.zcentric.com/uploads/ipodfile.jpg

Below is my PHP file I am using to handle uploads.

 

$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}";
}
 

 

It is nothing special. I do an echo so you can see in the console what the file is. If you switch the filename=”" part in the uploadImage section it will be placed as another file.

Files will be removed once they are 10 minutes old on the server so they won’t last but you can use it as a playground.

As always here is my code

分享到:
评论

相关推荐

    UIImage+ImageEffects

    1.UIImage+ImageEffects是Accelerate框架中的内容 2.UIImage+ImageEffects的模糊效果非常美观 3.修改过的UIImage+ImageEffects可以对图片进行局模糊

    Overwrites UIImageView & UIImage's description to print out the image in ASCII Art.zip

    Overwrites UIImageView & UIImage's description to print out the image in ASCII Art.zip,Overwrites UIImageView & UIImage's description to print out the image in ASCII Art

    UIImage-PDF.zip

    在iOS开发中,`UIImage`类是用于处理和显示图像的核心类,但默认情况下它并不支持PDF文档的显示。为了扩展`UIImage`的功能,开发者通常会创建分类(Category)来添加新的方法。`UIImage-PDF`就是这样一个Objective-C...

    OC Extension UIImage+FHXImage(图片扩展).zip

    OC Extension UIImage+FHXImage(图片扩展) 针对UIImage进行封装的工具类。 /** 输入图片颜色返回一张图片 */ + (UIImage *)createImageWithColor:(UIColor *)color; /** 裁切图片的一个点进行延伸 */ - ...

    UIImage+Sprite+Animmation

    在iOS开发中,`UIImage`是苹果提供的一个关键类,用于处理和显示图像。这个类扩展了`UIImage`,增加了精灵(Sprite)和动画功能,让开发者能够更灵活地创建和控制游戏或应用中的动态图像。`UIImage+Sprite+Additions...

    UIIMage 改变颜色和遮罩

    func applyMask(to image: UIImage, with mask: UIImage) -> UIImage? { let size = image.size guard let cgImage = image.cgImage else { return nil } let context = CGContext(data: nil, width: Int(size....

    WebP-UIImage-源码.rar

    这个“WebP-UIImage-源码.rar”压缩包可能包含了用Objective-C或Swift实现的iOS平台上对WebP图像格式的支持,使得开发者可以直接在UIImage对象中加载和显示WebP图片。 在iOS开发中,UIImage是苹果提供的一个关键类...

    IOS应用源码——UIImage+Sprite for iOS.rar

    在iOS开发中,UIImage是苹果提供的一个核心类,用于处理和显示图像。这个"UIImage+Sprite for iOS"的源码库显然扩展了UIImage类,增加了精灵(Sprite)功能,这通常用于2D游戏或者需要高效处理多帧动画的场景。在iOS...

    swift-给UIView或UIImage高性能添加圆角生成渐变色图片等

    在Swift编程语言中,对UIView或UIImage进行高性能的圆角处理和生成渐变色图片是常见的图像操作。这些操作在用户界面设计中尤其重要,因为它们可以提升应用的视觉效果和用户体验。以下将详细介绍如何实现这些功能。 ...

    iOS图片压缩---UIImage+Wechat

    UIImage+Wechat 是一个专门用于图片压缩的优秀框架,旨在帮助开发者有效地减小图片的大小,同时保持较高的图像质量。这个框架在处理图片时,不仅关注压缩比例,还着重于保持图片的像素质量,以避免过多的图像失真。 ...

    iOS 录制视频流 转uiimage

    在iOS开发中,录制视频流并将其转换为UIImage是一项常见的需求,这通常涉及到多媒体处理、图形编程以及实时渲染等技术。下面将详细讲解这个过程涉及的知识点。 首先,我们需要了解如何在iOS上进行实时视频录制。...

    ios开发UIImage category实现添加水印(图片和文字)

    UIImage Category是一种优雅的方式,可以扩展UIImage的功能,无需修改原生类,方便地实现水印功能。本教程将深入讲解如何通过Category实现UIImage的水印功能。 首先,让我们了解Category的概念。Category是...

    UIImage-BlurredFrame, UIImage类别,模糊了UIImage的指定框架.zip

    UIImage-BlurredFrame, UIImage类别,模糊了UIImage的指定框架 UIImage+BlurredFrame是一个UIImage类别,模糊了UIImage的指定框架#Install使用 cocoapods 安装pod 'UIImage+BlurredFrame'#Usage

    UIImage 图片处理:截图,缩放,设定大小,存储_蓝科教育

    -(UIImage*)scaleImage:(UIImage *)image toScale:(float)scaleSize{ UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize)); [image drawInRect:CGRectMake(0,...

    颜色创建图像工具 UIImage-ImageWithColor.zip

    颜色创建图像工具 UIImage-ImageWithColor ,UIImage-ImageWithColor 是一款使用颜色创建图像的工具

    ios UIImage category 图片加载

    在iOS开发中,`UIImage`类是处理图片的核心组件,用于加载、显示和操作图片。在实际应用中,我们经常需要高效地加载图片以优化内存使用和应用性能。标题提到的"ios UIImage category 图片加载"是关于如何通过分类...

    NSData 与 NSString,Byte数组,UIImage 的相互转换

    在iOS和macOS开发中,`NSData`、`NSString`以及`UIImage`是常见的数据类型,它们在不同的场景下有着各自的用途。理解并熟练掌握它们之间的转换是非常重要的,这有助于我们处理各种数据和资源。下面将详细介绍这些...

Global site tag (gtag.js) - Google Analytics