1.上传图片到七牛会需要一个上传凭证,该上传token公司的后台会提供,我们只需要用根据后台提供的接口,请求到我们需要的上传token。然后根据七牛提供的方法QNUploadManager进行图片上传。上传成功会获取到图片的key
2.实现思路:从相册选择图片后,把图片的data存储到一个的数组中,然后用for循环遍历获取到每张图片的data 代码:
-(void)getTokenFromQN{
inti =0;
for(NSData*data1in_textView.allDatats) {
//生成图片的名字key
NSString*key = [NSStringstringWithFormat:@"%@-%d",[self imageKey],i];
NSString* userId = [[NSUserDefaultsstandardUserDefaults]objectForKey:USERID];
intuid = [userId intValue];
NSString*token = [[NSUserDefaults standardUserDefaults]objectForKey:TOKEN];
NSMutableDictionary*params = [[NSMutableDictionaryalloc]init];
params[@"uid"]=@(uid);
params[@"token"]=token;
[[ToolNetsharedToolNet]POST:UPTOKEN_URLparameters:paramssuccess:^(id_NonnullresponseObject) {
NSDictionary*data = [response ObjectobjectForKey:@"data"];
self.qnToken= [dataobject ForKey:@"token"];
if(![_textView.videoData containsObject:data1]) {
[self uploadImageToQNFilePath:data1key:keytoken:_qnToken];
}else{
[self uploadImageToQNFilePath:data1key:_videoKeytoken:_qnToken];
}
DLog(@"===%@",_qnToken);
}failure:^(NSError*_Nonnullerror) {
}];
i++;
}
3.根据七牛提供的方法上传图片
-(void)uploadImageToQNFilePath:(NSData*)data key:(NSString*)key token:(NSString*)token{
QNUploadManager*upManager = [[QNUploadManager alloc]init];
QNUploadOption*opt = [[QNUploadOption alloc]initWithMime:nil
progressHandler:^(NSString*key,float percent)//上传进度
{
self.percentFloat= percent;
DLog(@"**%.2f",percent);
}params:@{@"x:foo":@"fooval"}checkCrc:YEScancellationSignal:nil];
[upManagerputData:datakey:keytoken:tokencomplete: ^(QNResponseInfo*info,NSString*key,NSDictionary*resp) {
if(!info.error)
{
[_dataArr addObject:key];//存储图片名称的key,上传七牛需要用到
DLog(@"dataArr=%@",_dataArr);
if(_textView.imageArray.count==_dataArr.count) {
DLog(@"dataArr=%@",_dataArr);
[self upALLImage];
[WSProgressHUD showImage:nilstatus:@"上传成功"];
}
}else{
NSLog(@"****%@",info.error);
}
}option:opt];
self.timer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(getPercente)userInfo:nilrepeats:YES];
}
//根据时间戳标示图片的key,防止图片上传重复或者失败
- (NSString*)imageKey {
//根据当前系统时间生成图片名称
NSDate*date = [NSDatedate];
longtime=(long)[datetimeIntervalSince1970];//获取时间戳
return[NSStringstringWithFormat:@"%ld",time];
}
网友评论