iOS AFNetWorking 3.0的HTTPS上传多张图片

基于AFNetWorking 3.0的上传多张图片的方法,这里只列出关键代码

 AFHTTPSessionManager*manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kSuppliersHttpServer]];
    manager.requestSerializer.timeoutInterval = 15;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = nil;
   // HTTPS 请求设置证书
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    //设置证书模式, xman_cert_file是cer证书名
    NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xman_cert_file" ofType:@"cer"];
    NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
    
    //NSLog(@"cerData===%@",cerData);
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:[[NSSet alloc] initWithObjects:cerData, nil]];
    // 客户端是否信任非法证书
    manager.securityPolicy.allowInvalidCertificates = YES;
    // 是否在证书域字段中验证域名
    [manager.securityPolicy setValidatesDomainName:NO];

    [manager POST:cmd_UploadImg parameters:nil constructingBodyWithBlock:^(id  _Nonnull formData) {
        // 关键代码
        for (int i = 0; i < imageArr.count; i++) {
            UIImage *image = imageArr[i];
            CGSize imageSize;
            imageSize.width = 720; //改变图片宽高
            imageSize.height = 540;
            UIImage *sizeImage = [self imageWithImage:image scaledToSize:imageSize];
           
             NSData *imageData = UIImageJPEGRepresentation(sizeImage,1);// 该改变照片的大小 100k->50K
            
//           float length = [imageData length]/1024;
//            NSLog(@"length=====%f",length);//获取图片的 大小kb
            
            // 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
            // 要解决此问题,
            // 可以在上传时使用当前的系统事件作为文件名
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            // 设置时间格式
            [formatter setDateFormat:@"yyyyMMddHHmmss"];
            NSString *dateString = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString  stringWithFormat:@"%@.jpg", dateString];
            /*
             *该方法的参数
             1. appendPartWithFileData:要上传的照片[二进制流]
             2. name:对应网站上[upload.php中]处理文件的字段(比如upload)
             3. fileName:要保存在服务器上的文件名
             4. mimeType:上传的文件的类型
             */
            [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
        }
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"上传进度 uploadProgress ===%@", uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       NSError *error = nil;
        id dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        NSLog(@"上传成功 dic ===%@",dic);
}];

下面是处理图片的方法

-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}

你可能感兴趣的:(iOS AFNetWorking 3.0的HTTPS上传多张图片)