/**
*  上传带图片的内容,允许多张图片上传(URL)POST
*
*  @param url                 网络请求地址
*  @param images              要上传的图片数组(注意数组内容需是图片)
*  @param parameter           图片数组对应的参数
*  @param parameters          其他参数字典
*  @param ratio               图片的压缩比例(0.0~1.0之间)
*  @param succeedBlock        成功的回调
*  @param failedBlock         失败的回调
*  @param uploadProgressBlock 上传进度的回调
*/
+(void)startMultiPartUploadTaskWithURL:(NSString *)url
                           imagesArray:(NSArray *)images
                     parameterOfimages:(NSString *)parameter
                        parametersDict:(NSDictionary *)parameters
                      compressionRatio:(float)ratio
                          succeedBlock:(void(^)(id operation, id responseObject))succeedBlock
                           failedBlock:(void(^)(id operation, NSError *error))failedBlock
                   uploadProgressBlock:(void(^)(float uploadPercent,long long totalBytesWritten,long long totalBytesExpectedToWrite))uploadProgressBlock;

实现:
+(void)startMultiPartUploadTaskWithURL:(NSString *)url
                           imagesArray:(NSArray *)images
                     parameterOfimages:(NSString *)parameter
                        parametersDict:(NSDictionary *)parameters
                      compressionRatio:(float)ratio
                          succeedBlock:(void (^)(id, id))succeedBlock
                           failedBlock:(void (^)(id, NSError *))failedBlock
                   uploadProgressBlock:(void (^)(float, long long, long long))uploadProgressBlock{
    
    if (images.count == 0) {
        NSLog(@"上传内容没有包含图片");
        return;
    }
    for (int i = 0; i < images.count; i++) {
        if (![images isKindOfClass:[UIImage class]]) {
            NSLog(@"images中第%d个元素不是UIImage对象",i+1);
            return;
        }
    }
    
    AFHTTPRequestOperation *operation = [[self sharedOperation].operationManager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        int i = 0;
        //根据当前系统时间生成图片名称
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy年MM月dd日"];
        NSString *dateString = [formatter stringFromDate:date];
        
        for (UIImage *image in images) {
            NSString *fileName = [NSString stringWithFormat:@"%@%d.png",dateString,i];
            NSData *imageData;
            if (ratio > 0.0f && ratio < 1.0f) {
                 imageData = UIImageJPEGRepresentation(image, ratio);
            }else{
                imageData = UIImageJPEGRepresentation(image, 1.0f);
            }
           
            [formData appendPartWithFileData:imageData name:parameter fileName:fileName mimeType:@"image/jpg/png/jpeg"];
        }
    
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succeedBlock(operation,responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        failedBlock(operation,error);
        
    }];
    
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        CGFloat percent = totalBytesWritten * 1.0 / totalBytesExpectedToWrite;
        uploadProgressBlock(percent,totalBytesWritten,totalBytesExpectedToWrite);
    }];
    

 

iOS - (多图上传已封装)的更多相关文章

  1. iOS多图上传

    iOS多图上传涉及到多线程问题,个人比较喜欢使用GCD操作,下边是最近写的一个多图上传代码,附带相关注释 __block BOOL allSucc = YES; __block int m = 0; ...

  2. iOS 使用AFN 进行单图和多图上传 摄像头/相册获取图片,压缩图片

    图片上传时必要将图片进行压缩,不然会上传失败 首先是同系统相册选择图片和视频.iOS系统自带有UIImagePickerController,可以选择或拍摄图片视频,但是最大的问题是只支持单选,由于项 ...

  3. iOS 使用AFN 进行单图和多图上传

    图片上传时必要将图片进行压缩,不然会上传失败 1.单张图上传 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManag ...

  4. 微信企业号办公系统-JSSDK上传图片(多图上传)

    在开发微信企业号办公系统中,涉及到了图片上传功能,一开始使用的flash插件上传方法,在苹果手机上可以调用相机直接拍摄照片,但在安卓手机上只能选择照片. 微信jssdk-api带有一套完整的调用选择本 ...

  5. 【iOS 使用github上传代码】详解

    [iOS 使用github上传代码]详解 一.github创建新工程 二.直接添加文件 三.通过https 和 SSH 操作两种方式上传工程 3.1https 和 SSH 的区别: 3.1.1.前者可 ...

  6. iOS上架ipa上传问题那些事

    iOS上架ipa上传问题那些事 原文: http://www.jianshu.com/p/1e22543285c2 字数513 阅读312 评论0 喜欢1 通过xcode直接打包上传,不会提示你的ip ...

  7. 微信jssdk,实现多图上传的一点心得

    一.首先在common.js里封装一个函数,在需要调用jsSDK的页面引用此方法即可实现微信的信息配置function signatureJSSDK() { var url = window.loca ...

  8. JS案例之4——Ajax多图上传

    近期项目中有好几次用到多图上传,第一次在项目中真正用到Ajax技术,稍微整理了下,贴个案例出来. 我们传统的做法是当用户提交一个表单时,就向web服务器端发送一个请求.服务器接受并处理传来的表单信息, ...

  9. 怎么样通过php使用html5实现多文件上传?(php多图上传)

    <!DOCTYPE html><html lang="zh-cn"> <head> <meta charset="utf-8&q ...

随机推荐

  1. A little issue in Mathematical Thought from Ancient to Modern Times, Vol. 3

    At P985 of the book, says But there are cuts that are not determined by rational numbers. If we put ...

  2. day3:数据类型 str

    1,int 一个数字占用的bit数目 i = 2 print(i.bit_length()) i = 3 print(i.bit_length()) i = 5 print(i.bit_length( ...

  3. MathType怎么打定积分竖线

    MathType怎么打定积分竖线-MathType中文官网 http://www.mathtype.cn/jiqiao/dingjifen-shuxian.html 输入公式后在分隔符模板中选择左竖线 ...

  4. MySQL数据类型之字符串类型

    CHAR(M) 分配固定长度的空间建(字节) VARCHAR(M)按照实际长度存储(占用的字节数是实际的长度加上1或者2)当M的值小于255时,加一.    当M的值大于255时,小于65532时,加 ...

  5. mysql之workbench如何只导出(insert语句)数据

    https://www.jianshu.com/p/a5cd14bc5499 1. 说明: 出发点: 由于特殊原因,我们只想导出数据库中的数据(insert into语句格式的),但是在网上找到的资源 ...

  6. java 线程 (三)线程并发的安全性 同步代码块

    package cn.sasa.demo1; import java.util.concurrent.ExecutionException; public class ThreadDemo { pub ...

  7. 牛客Wannafly9E 组一组 差分约束

    正解:差分约束 解题报告: 传送门! 首先肯定要想到把他们分开来考虑,就是说,把数二进制拆分掉,这样就可以分开考虑了嘛 然后考虑设f[i]:前i个数中的1的个数 然后就可以得到一堆差分约束的式子 然后 ...

  8. Java中String类两种实例化的区别(转)

    原文:http://blog.csdn.net/wangdajiao/article/details/52087302 一.String类的第一种方式 1.直接赋值 例:String str = &q ...

  9. VS Code编辑器对git项目的支持

    使用git随便clone一个项目下来, 然后用vscode打开项目, 随便打开某个文件, 添加几行代码: 9-11行是我新添加的, 左边绿色的竖条(点击就会看到明细)就表示这几行是新添加的. 然后修改 ...

  10. nessus的安装

    nessus安装 .下载地址 http://www.tenable.com/products/nessus/select-your-operating-system .获取注册码 www.nessus ...