上传单张图片
//上传图片(单张)
+(void)uploadPhotoAndController:(UIViewController *)controller WithSize:
(CGSize)size Image:(UIImage*)image AndFinish:(void (^)(NSDictionary *, NSError *))finish
{
//加载提示菊花
MBProgressHUD *hud;
if(controller) {
hud = [MBProgressHUD showHUDAddedTo:controller.view animated:YES];
hud.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
}
//1. 存放图片的服务器地址,这里我用的宏定义
NSString * url = [NSString stringWithFormat:@"%@%@",Hx_Main_heard_API,IMAGE_UPLOAD_URL_API];
//2. 利用时间戳当做图片名字
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *imageName = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.jpg",imageName];
//3. 图片二进制文件
NSData *imageData = UIImageJPEGRepresentation(image, 0.7f);
NSLog(@"upload image size: %ld k", (long)(imageData.length / 1024));
//4. 发起网络请求
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 上传图片,以文件流的格式,这里注意:name是指服务器端的文件夹名字,
//参数fileName可以是任意以.jpg/.jpeg/.png/.gif结尾的字符串,
//参数mimeType可以是@"image.jpg/@"image.jpeg"/@"image.png"/@" image.gif"中的任意一个
[formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
//上传成功时的回调
[hud hideAnimated:YES];
NSLog(@"%@",responseObject);
finish(responseObject,nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//失败时的回调
[hud hideAnimated:YES];
finish(nil,error);
}];
}
// 上传图片(多张)
+(void)uploadPhotoAndController:(UIViewController *)controller WithSize:(CGSize)size Image:(UIImage*)image AndFinish:(void (^)(NSDictionary *, NSError *))finish
{
//加载提示菊花
MBProgressHUD *hud;
if(controller){
hud = [MBProgressHUD showHUDAddedTo:controller.view animated:YES];
hud.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
}
//1. 存放图片的服务器地址,这里我用的宏定义
NSString * url = [NSString stringWithFormat:@"%@%@",Hx_Main_heard_API,IMAGE_UPLOAD_URL_API];
//2. 发起网络请求
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
// 上传多张图片
for(NSInteger i = 0; i < self.imageDataArray.count; i++)
{
//取出单张图片二进制数据
NSData * imageData = self.imageDataArray[i];
// 上传的参数名,在服务器端保存文件的文件夹名,这个需要与后台商量好,可能是保存在同一个文件夹下如:@"pic",这时下参数就是固定的name:@"pic"
NSString * Name = [NSString stringWithFormat:@"%@%ld", Image_Name, i+1];
// 上传filename
NSString * fileName = [NSString stringWithFormat:@"%@.jpg", Name];
[formData appendPartWithFileData:imageData name:Name fileName:fileName mimeType:@"image/jpeg"];
}
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
//上传成功时的回调
[hud hideAnimated:YES];
NSLog(@"%@",responseObject);
finish(responseObject,nil);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
//失败时的回调
[hud hideAnimated:YES];
finish(nil,error);
}];
}
网友评论