- 浏览: 773847 次
- 性别:
- 来自: 天堂
文章分类
最新评论
-
xiaozhao-521:
呀呀呀呀呀呀呀
RequestTest222 -
Andy_hyh:
打扰了,问下openmeeting源码可以运行起来吗?
Openmeetings安装 详细步骤 -
qindongliang1922:
擦,现在还行么,厉害
北京免费吃饭的地方 -
minixx77:
...
Openmeetings安装 详细步骤 -
wwwqqqiang:
喜欢楼主分享问题的方式,有思想
UIView 和 CALayer的那点事
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.
- qqwebservice.rar (32.7 KB)
- 下载次数: 14
- axis_webservice_本地代理类生产.rar (4.8 MB)
- 下载次数: 10
发表评论
-
iOS 自定义UIActionSheet
2012-12-18 16:07 16424一:模态视图 UIActi ... -
UIView 和 CALayer的那点事
2012-11-17 23:51 30782UIView 和 CALayer的那点事 (1 ... -
iOS Open Source : Popover API for iPhone
2012-01-20 15:02 1948http://iphonedevelopertips.com/ ... -
ios 任务、线程、定时器
2011-12-26 18:09 8032一:operations(任务) cocoa提供了三种 ... -
ios url缓存策略——NSURLCache、 NSURLRequest
2011-12-26 17:09 24360一:url 缓存策略 NSURLRequest ... -
ios NSInvocation简单使用
2011-12-22 16:39 6379在ios直接调用某个对象的消息是方法有两种: 一:perfo ... -
iphone 对Web Services的三种请求方式soap get post
2011-11-09 10:57 6444一:Using SO AP 1.1 POST / ... -
sdk3.2手势实例
2011-11-09 10:11 1747#import <UIKit/UIKit.h>@i ... -
关于iphone 利用hpple解析html的问题
2011-08-04 18:28 2229最近在用happe解析html中的图片。有个翻页操作,如果请 ... -
iphone hpple 解析html,xml
2011-07-19 16:21 2755使用Objective-C解析HTML或者XML,系统自带有两 ... -
激活 iPhone通过 GPRS 连接服务器功能的代码
2011-05-13 15:14 1663如果您的 iPhone 应用里含有连接服务器的功能,也许会遇到 ... -
address book api 图型
2011-04-28 15:51 1151最近要搞地址簿了,整理一下 -
[OmniGraffle]iPhone app原型制作工具
2011-04-06 17:35 3962在写程序之前,我们通常需要做一些mockup出来(不知道款爷有 ... -
自定义uislider 样式
2011-04-04 21:28 3844UIImage *stetchLeftTrack= [[UII ... -
iphone 下AsyncSocket网络库编程
2011-04-02 21:04 7647iphone的标准推荐CFNetwork ... -
进阶AlertView运用 - 登入设计
2011-04-01 17:52 3043说明:示范如何利用AlertView来制作系统登入的介面程式碼 ... -
iPad UIPopoverController弹出窗口的位置和坐标
2011-04-01 17:42 2008优化规则: TodoViewControlle ... -
iPhone系统自动化测试
2011-04-01 17:39 2624首先mac系统是必备的2 安装iPhone SD ... -
iphone上面编写具有root权限的程序
2011-04-01 17:31 6302正常途径下, 我们编写的程序发布在App store上, 使用 ... -
聊天。。。。。
2011-04-01 17:13 1095是得分手段
相关推荐
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
在iOS开发中,`UIImage`类是用于处理和显示图像的核心类,但默认情况下它并不支持PDF文档的显示。为了扩展`UIImage`的功能,开发者通常会创建分类(Category)来添加新的方法。`UIImage-PDF`就是这样一个Objective-C...
OC Extension UIImage+FHXImage(图片扩展) 针对UIImage进行封装的工具类。 /** 输入图片颜色返回一张图片 */ + (UIImage *)createImageWithColor:(UIColor *)color; /** 裁切图片的一个点进行延伸 */ - ...
在iOS开发中,`UIImage`是苹果提供的一个关键类,用于处理和显示图像。这个类扩展了`UIImage`,增加了精灵(Sprite)和动画功能,让开发者能够更灵活地创建和控制游戏或应用中的动态图像。`UIImage+Sprite+Additions...
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”压缩包可能包含了用Objective-C或Swift实现的iOS平台上对WebP图像格式的支持,使得开发者可以直接在UIImage对象中加载和显示WebP图片。 在iOS开发中,UIImage是苹果提供的一个关键类...
在iOS开发中,UIImage是苹果提供的一个核心类,用于处理和显示图像。这个"UIImage+Sprite for iOS"的源码库显然扩展了UIImage类,增加了精灵(Sprite)功能,这通常用于2D游戏或者需要高效处理多帧动画的场景。在iOS...
在Swift编程语言中,对UIView或UIImage进行高性能的圆角处理和生成渐变色图片是常见的图像操作。这些操作在用户界面设计中尤其重要,因为它们可以提升应用的视觉效果和用户体验。以下将详细介绍如何实现这些功能。 ...
UIImage+Wechat 是一个专门用于图片压缩的优秀框架,旨在帮助开发者有效地减小图片的大小,同时保持较高的图像质量。这个框架在处理图片时,不仅关注压缩比例,还着重于保持图片的像素质量,以避免过多的图像失真。 ...
在iOS开发中,录制视频流并将其转换为UIImage是一项常见的需求,这通常涉及到多媒体处理、图形编程以及实时渲染等技术。下面将详细讲解这个过程涉及的知识点。 首先,我们需要了解如何在iOS上进行实时视频录制。...
UIImage Category是一种优雅的方式,可以扩展UIImage的功能,无需修改原生类,方便地实现水印功能。本教程将深入讲解如何通过Category实现UIImage的水印功能。 首先,让我们了解Category的概念。Category是...
UIImage-BlurredFrame, UIImage类别,模糊了UIImage的指定框架 UIImage+BlurredFrame是一个UIImage类别,模糊了UIImage的指定框架#Install使用 cocoapods 安装pod 'UIImage+BlurredFrame'#Usage
-(UIImage*)scaleImage:(UIImage *)image toScale:(float)scaleSize{ UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize)); [image drawInRect:CGRectMake(0,...
颜色创建图像工具 UIImage-ImageWithColor ,UIImage-ImageWithColor 是一款使用颜色创建图像的工具
在iOS开发中,`UIImage`类是处理图片的核心组件,用于加载、显示和操作图片。在实际应用中,我们经常需要高效地加载图片以优化内存使用和应用性能。标题提到的"ios UIImage category 图片加载"是关于如何通过分类...
在iOS和macOS开发中,`NSData`、`NSString`以及`UIImage`是常见的数据类型,它们在不同的场景下有着各自的用途。理解并熟练掌握它们之间的转换是非常重要的,这有助于我们处理各种数据和资源。下面将详细介绍这些...