iOS 开发技巧心得体会

来源:互联网 发布:mac拷贝视频到iphone 编辑:程序博客网 时间:2024/05/16 15:47

iOS 传值 属性传值 :适应于push 前一个页面 向后一个页面传值  在前一个页面则赋值给后一个页面,  注意属性的申明 应放于 .h文件当中  

用AFNetworking 上传   

第一种  通过上传参数 base64

 NSString *str = [NSStringstringWithFormat:@"%@%@",HOME_BANNER_URL,@"/Refund/uploadVoucher"];

       NSDictionary *subDict =@{@"token":[IWUserDefaultsobjectForKey:@"token"],@"baseData":baseStr,@"ext":@"jpeg",@"type":@(2)};

    AFHTTPRequestOperationManager *manager =[[AFHTTPRequestOperationManageralloc]init];

    NSDictionary *parameters =subDict;

    [manager POST:strparameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData_Nonnull formData) {


       // [formData appendPartWithFileURL:fileP  name: imageName error:nil];

        

    } success:^(AFHTTPRequestOperation *_Nonnull operation, id  _Nonnull responseObject) {

        NSLog(@"Success :%@",responseObject);

    } failure:^(AFHTTPRequestOperation *_Nullable operation, NSError * _Nonnull error) {

        NSLog(@"Error :%@",error);

    }];

第二种  无参数  表格上传file  链接地址 : http://www.tuicool.com/articles/JFJrue

 NSURLRequest *request = [_httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {14         // 在此位置生成一个要上传的数据体15         // form对应的是html文件中的表单16         17         18         UIImage *image = [UIImage imageNamed:@"头像1"];19         NSData *data = UIImagePNGRepresentation(image);20         21         // 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名22         // 要解决此问题,23         // 可以在上传时使用当前的系统事件作为文件名24         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];25         // 设置时间格式26         formatter.dateFormat = @"yyyyMMddHHmmss";27         NSString *str = [formatter stringFromDate:[NSDate date]];28         NSString *fileName = [NSString stringWithFormat:@"%@.png", str];29         30         31         /*32          此方法参数33          1. 要上传的[二进制数据]34          2. 对应网站上[upload.php中]处理文件的[字段"file"]35          3. 要保存在服务器上的[文件名]36          4. 上传文件的[mimeType]37          */38         [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/png"];39     }];40     41     // 3. operation包装的urlconnetion42     AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];43     44     [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {45         NSLog(@"上传完成");46     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {47         NSLog(@"上传失败->%@", error);48     }];49     50     //执行51     [_httpClient.operationQueue addOperation:op];

 


1 0