1. ASI框架后期都没有更新了,需要注意
ASIFormDataRequest *uploadImageRequest= [ ASIFormDataRequest requestWithURL : [NSURL URLWithString:[[NSString stringWithFormat:@"%@Works/release",HaoSe_URL] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ]];
[uploadImageRequest setStringEncoding:NSUTF8StringEncoding];
[uploadImageRequest setRequestMethod:@"POST"];
//有些接口要token要放在头里的应该这样写
[uploadImageRequest addRequestHeader:@"token" value:TOKEN];
//传的类似是字典,要转换成对象
[uploadImageRequest setPostValue:[NSNumber numberWithInt:[USERID intValue]] forKey:@"user_id"];
if (NULL_STR(_descStr)) {
[uploadImageRequest setPostValue:@"" forKey:@"title"];
}else{
[uploadImageRequest setPostValue:_descStr forKey:@"title"];
}
[uploadImageRequest setPostValue:[NSNumber numberWithInt:(int)copyright] forKey:@"copyright"];
[uploadImageRequest setPostValue:[NSNumber numberWithInt:(int)related_activity] forKey:@"related_activity"];
[uploadImageRequest setPostValue:[NSNumber numberWithInt:(int)related_point] forKey:@"related_shoot"];
//这个地方是传图片,重要的代码在这里
if (array.count > 0) {
for (int i = 0; i < array.count ;i++) {
UIImage *image = nil;
ZLPhotoAssets *asset = [array objectAtIndex:i];
if ([asset isKindOfClass:[ZLPhotoAssets class]]) {
image = asset.originImage;
}else if ([asset isKindOfClass:[NSString class]]){
}else if([asset isKindOfClass:[UIImage class]]){
image = (UIImage *)asset;
}
NSData *imageData = UIImageJPEGRepresentation(image, 0.2);
//这个是核心代码
[uploadImageRequest addData:imageData forKey:@"images[]"];
}
}
[uploadImageRequest setPostFormat:ASIMultipartFormDataPostFormat];
[uploadImageRequest setDelegate:self];
[uploadImageRequest setDidFinishSelector : @selector (responseComplete:)];
[uploadImageRequest setDidFailSelector : @selector (responseFailed:)];
[uploadImageRequest startAsynchronous];
上传后的回调
-(void)responseComplete:(ASIHTTPRequest*)request{
NSString *responseString = [request responseString];
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:nil];
NSLog(@"上传成功。。。。。。。%@---%@",responseString,result);
}
-(void)responseFailed:(ASIHTTPRequest*)request{
NSString *responseString = [request responseString];
NSLog(@"上传失败。。。。。。。%@",responseString);
}