用AFNetworking上传图片
其实我弄了好久,也没弄好多张一起上传,只好弄单张上传,如果你看到这篇文章对您有帮助的话欢迎您及时完善我的信息,也让我学习一下多张上传怎么弄,谢谢
//首先实例化一个AFHTTPRequestOperationManager操作对象
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"上传图片的接口"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"logo1.jpg"];
NSData *logoJpg = [NSData dataWithContentsOfFile:documentsDirectory];
UIImage * image = [UIImage imageWithData:logoJpg];
上面是从手机路径中取图片
// NSURL *filePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"avatar.jpg" ofType:nil]];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);//压缩
UIImage *image = [UIImage imageNamed:@"头像1"];
NSData *data = UIImagePNGRepresentation(image);
// 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
// 要解决此问题,
// 可以在上传时使用当前的系统事件作为文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置时间格式
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
/*
此方法参数
1. 要上传的[二进制数据]
2. 对应网站上[upload.php中]处理文件的[字段"file"]
3. 要保存在服务器上的[文件名]
4. 上传文件的[mimeType]
*/
if (imageData) {
NSDictionary * dic1 = @{};//接口要求的参数
NSMutableURLRequest *request = [manager.requestSerializer
multipartFormRequestWithMethod:@"POST"
URLString:@"http://yapi.chelehuo.net/upload"
parameters:dic1
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:@"logo1"
fileName:@"logo1.jpg"
mimeType:@"image/jpeg"];
}
error:nil];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation1:%@", responseObject);
弄你想要的参数
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure. upload....");
}];
[manager.operationQueue addOperation:operation];
//可以查看上传的进度
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"百分比:%f",totalBytesWritten*1.0/totalBytesExpectedToWrite);
}];
}
多张图片上传的方法
方法一:
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:url parameters:url参数 constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (int i = 0; i<arrayImage.count; i++) {
UIImage *uploadImage = arrayImage[i];
[formData appendPartWithFileData:UIImagePNGRepresentation(uploadImage)name:[NSString stringWithFormat:@"参数%d",i+1] fileName:@"test.jpg"mimeType:@"image/jpg"];
}
} error:nil];
AFHTTPRequestOperation *opration = [[AFHTTPRequestOperationalloc]initWithRequest:request];
opration.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];
[opration setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, idresponseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}
这个多张上传我是失败的不知道为啥这是我在网上看到的几个博客里的例子,可以看看
http://blog.csdn.net/liuwuguigui/article/details/40142739
http://blog.sina.com.cn/s/blog_a776fc650102uyvb.html
http://www.tuicool.com/articles/JFJrue
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。