QiniuUpload 3.0.0

QiniuUpload 3.0.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2017年8月

huhuanming 维护。



  • huhuanming

qiniu_upload 是一款支持七牛云存储的 iOS/macOS sdk。

除了文件上传等基本功能外,qiniu_upload 还实现了多文件队列上传。

qiniu_upload 进入维护,想去编写 UploadKit。

完成

  • [x] 减小内存占用,清除内存泄漏
  • [x] 支持多种数据来源,包括 ALAsset, NSData,NSFileManager
  • [x] 支持 NSInputStream 上传方式
  • [x] 支持并发上传
  • [x] 支持在开发环境中提示版本更新
  • [x] 删除所有警告
  • [x] 删除 AFNetworking 支持

如何开始


Podfile

    pod "QiniuUpload"

手动安装

将 Classes 目录下的类复制到您的工程项目中即可。

开始编码

QiniuToken

首先初始化一个 QiniuToken。scope、secretKey、accessKey 在注册七牛后官方都会给出

    [QiniuToken registerWithScope:@"your_scope" SecretKey:@"your_secretKey" Accesskey:@"your_accesskey"];

这样初始化,一个 Token 的默认有效期是5分钟,如果您想自定义生命周期,可以这样初始化

    [QiniuToken registerWithScope:@"your_scope" SecretKey:@"your_secretKey" Accesskey:@"your_accesskey" TimeToLive:60]

生成一个上传凭证

    NSString *uploadToken = [[QiniuToken sharedQiniuToken] uploadToken]

不建议在生产环境的代码中直接填写 accesskey 和 secretKey 来使用。

QiniuFile

初始化要上传的七牛文件,图片、音频等都可以。

以下以图片为例(使用 NSData 方法)

    QiniuFile *file = [[QiniuFile alloc] initWithFileData:UIImageJPEGRepresentation(your_image, 1.0f)];

或者一段音频(使用路径方法)

    NSString *path = [NSString stringWithFormat:@"%@/%@",[NSBundle mainBundle].resourcePath,@"your_mp3"];
    QiniuFile *file = [[QiniuFile alloc] initWithPath:path];

或者 ALAsset

    QiniuFile *file = [[QiniuFile alloc] initWithAsset: your_asset]];

QiniuUploader

添加文件

    uploader.files = @[file, file, file];

这里的 QinniuFile 可以包含部分图片,部分视频、音频,不会对上传有影响。

设置并发上限

    [[QiniuUploader sharedUploader] setMaxConcurrentNumber:3];

建议不要设置过大,因为 iOS 的连接数是有限的。

开始上传

完成上述设置后,调用这个函数开始上传。

    [uploader startUpload:uploadToken uploadOneFileSucceededHandler:^(NSInteger index, NSDictionary * _Nonnull info) {
            NSLog(@"index: %ld info: %@",(long)index, info);
        } uploadOneFileFailedHandler:^(NSInteger index, NSError * _Nullable error) {
            NSLog(@"error: %@", error);
        } uploadOneFileProgressHandler:^(NSInteger index, int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
             NSLog(@"index:%ld percent:%f",(long)index, totalBytesSent * 1.0 / totalBytesExpectedToSend);
        } uploadAllFilesComplete:^{
            NSLog(@"complete");
        }];

再次提醒,不要在应用内直接写入 key。

取消全部上传任务

当您希望取消所有上传任务时

    [uploader cancelAllUploadTask]

最后

如果还有不清楚的地方,可以看看 QiniuUploadDemo,里面什么都有。。。

如果您有希望加入的特性,可以在 issue 中留言。最后不要脸地求个 star...

谢谢

感谢 @pavelosipov

POSInputStreamLibrary 在解析 ALAsset 时节省了我大量时间

更新记录

CHANGELOG.md