首先要请求七牛云的token
- (void)loadSenvenToken{
NSString *url = [NSString stringWithFormat:@"%@%@",URL_Base,URL_CommentsToken];
[NetworkManager requestGETWithURLStr:url paramDic:nil Api_key:self.apikey finish:^(id responseObject) {
NSLog(@"token请求成功%@",responseObject);
self.token = [responseObject objectForKey:@"token"];
} enError:^(NSError *error) {
NSLog(@"失败%@",error);
}];
}
选择要上传的图片,完成后对图片进行处理,七牛云每次只能上传一张,所以要把对上传图片的处理写在添加图片方法完成后,这样每张图片都得到处理
- (void)loadAddPhotesViewWith:(UIView *)view{
// 只选择照片 addPhotoView 添加图片的view
HX_AddPhotoView *addPhotoView = [[HX_AddPhotoView alloc] initWithMaxPhotoNum:3 WithSelectType:SelectPhoto];
addPhotoView.backgroundColor = [UIColor whiteColor];
// 每行最大个数 不设置默认为4
addPhotoView.lineNum = 3;
// collectionView 距离顶部的距离 底部与顶部一样 不设置,默认为0
addPhotoView.margin_Top = 5;
// 距离左边的距离 右边与左边一样 不设置,默认为0
addPhotoView.margin_Left = 10;
// 每个item间隔的距离 如果最小不能小于5 不设置,默认为5
addPhotoView.lineSpacing = 5;
addPhotoView.delegate = self;
addPhotoView.frame = CGRectMake(0,65, size_width - 20, 0);
[view addSubview:addPhotoView];
/** 当前选择的个数 */
// addPhotoView.selectNum;
[addPhotoView setSelectPhotos:^(NSArray *photos, NSArray *videoFileNames, BOOL iforiginal) {
NSLog(@"photo - %@",photos);
[photos enumerateObjectsUsingBlock:^(id asset, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *twoAsset = (PHAsset *)asset;
CGFloat scale = [UIScreen mainScreen].scale;
// 根据输入的大小来控制返回的图片质
CGSize size = CGSizeMake(300 * scale, 300 * scale);
[[HX_AssetManager sharedManager] accessToImageAccordingToTheAsset:twoAsset size:size resizeMode:PHImageRequestOptionsResizeModeFast completion:^(UIImage *image, NSDictionary *info) {
self.img = image;
[self.photoList addObject:image];
NSLog(@"*********%@",image);// 选择几张走几次 3
[self getImagePath:image];
[self uploadImageToQNFilePath:[self getImagePath:self.img]];
}];
}];
}];
}
//照片获取本地路径转换
- (NSString *)getImagePath:(UIImage *)Image {
NSString *filePath = nil;
NSData *data = nil;
if (UIImagePNGRepresentation(Image) == nil) {
data = UIImageJPEGRepresentation(Image, 1.0);
} else {
data = UIImagePNGRepresentation(Image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *ImagePath = [[NSString alloc] initWithFormat:@"/theFirstImage.png"];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:ImagePath] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc] initWithFormat:@"%@%@", DocumentsPath, ImagePath];
return filePath;
}
- (void)uploadImageToQNFilePath:(NSString *)filePath {
// self.token = @"你的token";
QNUploadManager *upManager = [[QNUploadManager alloc] init];
QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
NSLog(@"percent == %.2f", percent);
}
params:nil
checkCrc:NO
cancellationSignal:nil];
[upManager putFile:filePath key:nil token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"info ===== %@", info);
NSLog(@"resp ===== %@", resp);
[self.keyArr addObject:[resp objectForKey:@"key"]];
} option:uploadOption];
}
保存事件
self.photoList 要上传的图片数组
self.keyArr 没上传一张图片成功后返回的key的数组
// 选择的图片都上传成功了
if (self.photoList.count == self.keyArr.count ) {
// 图片上传后,保存
[self loadProduct];// 提交给服务器 做的是评论
}else{
[self showMessageToUser:@"信息不完整"];
}
//保存
- (void)loadProduct{
NSString *url = [NSString stringWithFormat:@"%@%@",URL_Base,URL_CommentCreat];
NSDictionary *listDic = self.productList[0];
NSString *pID =[NSString stringWithFormat:@"%@",[listDic objectForKey:@"productId"]];
NSDictionary *paDic = @{@"productId":pID,@"CommentContent":self.textView.text,@"commentLevel":self.commenLabel.text,@"productCommentImage":self.keyArr};
NSMutableArray *arr = [NSMutableArray array];
[arr addObject:paDic];
// 数组转Json类型
NSData *data=[NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dic = @{@"orderId":self.orderId,@"productStoreId":self.productStoreId,@"comOrderType":self.type,@"commentList":jsonStr};
[NetworkManager requestPOSTWithURLStr:url paramDic:dic Api_key:self.apikey finish:^(id responseObject) {
NSLog(@"保存 请求成功%@",responseObject);
// 跳转到 上个界面
[self.commentDelegate updateData];
[self.navigationController popViewControllerAnimated:YES];
} enError:^(NSError *error) {
NSLog(@"失败%@",error);
}];
}