我通过官方文档写的代码,在调用putData上传函数时,回调block等了十几秒才调用,不是网络的问题(网络很快),就是反映特别慢,以下是我的代码:
先请求token,然后设置进度block,然后调用上传函数
__weak __typeof (&*self)weakSelf = self;
[NMEQiNiuTokenModel httpRequestWithRequestType:HTTPRequestTypeGet path:@"/qmm/near/qiniu/token" parameters:parameters handleBlock:^(NMEQiNiuTokenModel *tokenModel, NSError *error, FMIErrorModel *errorModel) {
uploadToken = tokenModel.upload_token;
uploadKey = tokenModel.key;
__strong __typeof (&*weakSelf)strongSelf = weakSelf;
//上传图片
QNUploadOption* progress = [[QNUploadOption alloc]initWithMime:nil progressHandler:^(NSString *key, float percent) {
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf.collectionDic[indexPathUpload] = [NSString stringWithFormat:@"%f",percent];
[strongSelf.imageCollectionView reloadData];
// if (percent == 1.0) {
// NSString *imageUrl = [NSString stringWithFormat:@"http://mmwd-client.m1img.com/%@",key];
// [strongSelf.imagesList addObject:imageUrl];
// }
});
NSLog(@"图片上传进度:%f",percent);
} params:nil checkCrc:NO cancellationSignal:nil];
strongSelf.upManager = [[QNUploadManager alloc] init];
[strongSelf.upManager putData:imageData key:uploadKey token:uploadToken complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
// [timer invalidate];
// timer = nil;
strongSelf.collectionDic[indexPathUpload] = @"1";
NSString *imageUrl = [NSString stringWithFormat:@"http://mmwd-client.m1img.com/%@",key];
[strongSelf.imagesList addObject:imageUrl];
strongSelf.collectionDic[indexPathUpload] = @"1.0";
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf.imageCollectionView reloadData];
});
} option:progress];
}];
七牛是不建议自己实现api的,因为七牛的上传已经有官方的SDK可以直接使用,而且针对大文件,IOS会自动进行分块上传。
http://developer.qiniu.com/docs/v6/sdk/objc-sdk.html
另外七牛的SDK已经集成七牛自己的happyDNS,七牛自动帮你解析节点IP,优化上传速度。