ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
iOS通过http post上传图片
iOS- 利用AFNetworking(AFN) - 实现文件上传
- (void)saveImage {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *UserID = [NSString stringWithFormat:@"%d",(int)delegate.userId];
NSString *ID = [NSString stringWithFormat:@"%d", _teamId];
NSString *Type = _teamType;
NSString *Intro = @"";
NSString *LinkAddr = @"";
if (_currIndex == 0) {
Intro = _bannerEditInfo.bannerIntro1;
LinkAddr = _bannerEditInfo.bannerLink1;
} else if (_currIndex == 1) {
Intro = _bannerEditInfo.bannerIntro2;
LinkAddr = _bannerEditInfo.bannerLink2;
} else {
Intro = _bannerEditInfo.bannerIntro3;
LinkAddr = _bannerEditInfo.bannerLink3;
}
NSString *HeadKey = [NSString stringWithFormat:@"Head%d", _currIndex+1];
NSString *sessionId = delegate.sessionId;
NSString *url = api_StudioManageHeadImageAlter;
NSDictionary *parameters = @{@"UserID":UserID,
@"ID":ID,
@"Type":Type,
@"Intro":Intro,
@"LinkAddr":LinkAddr,
@"HeadKey":HeadKey,
@"SessionId":sessionId};
[Helper printUrl:url parameters:parameters];
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 上传时使用当前的系统事件作为文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
//服务器上传文件的字段和类型
if (_currIndex == 0) {
NSData *data = [NSData dataWithContentsOfFile:_bannerEditInfo.banner1];
[formData appendPartWithFileData:data name:@"Head1" fileName:fileName mimeType:@"image/jpg/file"];
} else if (_currIndex == 1) {
NSData *data = [NSData dataWithContentsOfFile:_bannerEditInfo.banner2];
[formData appendPartWithFileData:data name:@"Head2" fileName:fileName mimeType:@"image/jpg/file"];
} else if (_currIndex == 2) {
NSData *data = [NSData dataWithContentsOfFile:_bannerEditInfo.banner3];
[formData appendPartWithFileData:data name:@"Head3" fileName:fileName mimeType:@"image/jpg/file"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self hideHud];
int errid = [responseObject[@"ErrId"] intValue];
if (errid != 1) {
[self showHint:@"保存失败"];
return;
}
[self showHint:@"保存成功"];
// [self.navigationController popViewControllerAnimated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self hideHud];
TTAlertForNetError(error.code);
}];
}
自己的单张循环上传
#pragma mark 图片上传功能的实现
- (void)saveBtnClick {
/**
以后做的话,就用单张循环上传,但是安卓那边就可以实现一次上传多张,这,为什么呢?
*/
#pragma mark 这些都是一些固定的部分
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求
manager.responseSerializer = [AFJSONResponseSerializer serializer];//响应
NSNumber *UserID =[NSNumber numberWithInteger:self.userid];
NSString *NickName = personalCell.nameTextFile.text;
NSString *Birthday = birthdayCell.brithdayTextFiled.text;
if (!Birthday) {
Birthday = [self getTime];
}
NSString *t = [BaseViewController millisecondsDifferential];
NSArray *photoIndexs = [self.addImageInformationDic allKeys];
#pragma mark 下面对变的参数进行赋值
for (NSNumber *photoIndex in photoIndexs) {
NSDictionary *dic2 = [self.addImageInformationDic objectForKey:photoIndex];
NSData *imageData = [[self.addImageInformationDic objectForKey:photoIndex] objectForKey:@"imageData"];
NSString *fileName = [[self.addImageInformationDic objectForKey:photoIndex] objectForKey:@"filePath"];
NSString *jiami = [BaseViewController md5:[NSString stringWithFormat:@"%@%@&UserID=%@&NickName=%@&Birthday=%@&PhotoName=%@&PhotoIndex=%@",kSecondTime,t,UserID,NickName,Birthday,dic2[@"photoName"],photoIndex]];
NSString *jiami1 = [BaseViewController Keymd5:jiami];
NSString *auth = [BaseViewController md5:jiami1];
//创建接口参数
NSDictionary *dic = @{@"t":t,
@"auth":auth,
// @"file":File,
@"UserId":UserID,
@"NickName":NickName,
@"Birthday":Birthday,
@"PhotoName":dic2[@"photoName"],
@"PhotoIndex":photoIndex
};
NSString *userPhotoUrl = [NSString stringWithFormat:@"%@%@",KTHttp,kUserAllPhoto];
[manager POST:userPhotoUrl parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//将数据传输到网上
[formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpg/Documents"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (!_letfVC) {
_letfVC = [[LeftVC alloc] init];
}
[_letfVC personHomepage];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"保存失败!");
}];
}
}