IOS上传图片的方法
来源:互联网 发布:小米任我行 知乎 编辑:程序博客网 时间:2024/05/16 09:31
下面是图片上传的方法:
-(void)loadImage:(NSString*)aurl
{
NSData *imageData;
NSMutableData *postBody;
NSString *stringBoundary, *contentType;
NSURL *url = [NSURL URLWithString:aurl]; //将字符串转换为NSURL格式
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"1.jpg"];
imageData = [[NSData alloc] initWithContentsOfFile:path];
ASIFormDataRequest *aRequest = [[ASIFormDataRequest alloc] initWithURL:url];
stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[aRequest addRequestHeader:@"Content-Type" value:contentType];
postBody = [[NSMutableData alloc] init];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post
NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttablexxxxxxxx"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[aRequest setDelegate:self];
[aRequest appendPostData:postBody];
[aRequest setRequestMethod:@"POST"];
[aRequest startAsynchronous];
[postBody release];
[aRequest release];
}
-(void)loadImage:(NSString*)aurl
{
NSData *imageData;
NSMutableData *postBody;
NSString *stringBoundary, *contentType;
NSURL *url = [NSURL URLWithString:aurl]; //将字符串转换为NSURL格式
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"1.jpg"];
imageData = [[NSData alloc] initWithContentsOfFile:path];
ASIFormDataRequest *aRequest = [[ASIFormDataRequest alloc] initWithURL:url];
stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[aRequest addRequestHeader:@"Content-Type" value:contentType];
postBody = [[NSMutableData alloc] init];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post
NSString *imageFileName = [NSString stringWithFormat:@"photo.jpeg"];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"upload\"; filename=\"%@\"\r\n",imageFileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"lighttablexxxxxxxx"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[aRequest setDelegate:self];
[aRequest appendPostData:postBody];
[aRequest setRequestMethod:@"POST"];
[aRequest startAsynchronous];
[postBody release];
[aRequest release];
}
- IOS上传图片的方法
- IOS上传图片的方法
- IOS上传图片的方法
- iOS上传图片的方法
- iOS开发上传图片的方法
- IOS上传图片方法类
- IOS上传图片方法类
- IOS上传图片方法类
- IOS上传图片方法类
- iOS 上传图片方法总结
- iOS 上传图片方法总结
- iOS图片上传-AFNetWorking的图片上传
- 图片上传的方法!
- 上传图片的方法
- 上传图片的方法
- iOS - 调用相机相册的方法 + 上传图片
- ios 表单上传图片和参数的2种方法
- IOS图片上传的代码
- Linux 驱动i2c -- Gsenser(三)
- 大规模插入操作防止表锁方法讨论
- (转载)短篇武侠《情剑断魂》
- (SQL Server)DBCC
- linux分支结构(case)
- IOS上传图片的方法
- 计算机中KB与Kb的技术差别详解
- java 代码规范
- c++中的代理类和句柄类
- 解决X64操作系统PL/SQL连接报错问题 make sure you have the 32 bits oracle client installed
- explode()函数的几个应用
- 浅谈系统需求调研
- 硬盘主引导记录,分区引导记录(MBR,PBR)
- 技术文档网址