我想从本地路径构建文件,需要用到AVFile里面的fileWithLocalPath:error: 这个接口,用的是chatKit 2.1.0版本,但是它依赖的AVOSCloud是8.0版本的,里面的AVFile没有从本地路径构建文件这个接口,我能用哪个函数代替?
因为我需要批量上传图片,所以不能直接把图片文件放在内存中,需要先把它们都放在缓存,然后再从本地上传到服务器。

你好,ChatKit 2.1.0 目前依赖的 AVOSCloud 为 8.2.3 版本,此版本中没有从本地构建文件接口。近期 ChatKit 会升级依赖最新版的 SDK,正在排期计划中。

在更新 SDK 之前的建议解决办法是,获取本地路径的图片后转换为 NSData 类型,调用 fileWithData 构建文件执行上传操作。

我查过在论坛里有关iOS批量上传文件问答,最后还是用了Dispatch Groups和AFFile 里面的fileWithName:contentsAtPath:来处理批量上传的问题,每上传成功一个文件,就把返回来的file.url放进一个数组里面。
但是这样又有个问题:因为是异步处理,返回来的file.url的顺序有可能被打乱,有没有什么解决方法?下面贴上现在写的部分代码:
-(void)uploadImage{
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *directory = [documentsDirectory stringByAppendingPathComponent:@"/MyProcess"];
NSError *error = nil;
//create group
dispatch_group_t group = dispatch_group_create();
//循环读取文件夹里面的每个文件名
for (NSString *fileName in [fm contentsOfDirectoryAtPath:directory error:&error]) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@", directory, fileName];
dispatch_group_enter(group);
AVFile *file = [AVFile fileWithName:fileName contentsAtPath:filePath];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
//urlList是用来放置返回来的file.url
[_urlList addObject:file.url];
dispatch_group_leave(group);
}];
}
//当全部文件上传成功后,调用该函数
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"finally!");
});
}

fileWithData是把文件暂时放在内存里面吗?如果是批量上传文件,在for循环中上传文件,会不会占用过多内存?用AVFile fileWithName: contentsAtPath:的效果是不是跟fileWithData一样?

因为是异步处理,返回来的file.url的顺序有可能被打乱,有没有什么解决方法?

这个顺序问题要根据您自己的需求自己来解决一下。

fileWithData是把文件暂时放在内存里面吗?如果是批量上传文件,在for循环中上传文件,会不会占用过多内存?用AVFile fileWithName: contentsAtPath:的效果是不是跟fileWithData一样?

可以用 fileWithName: contentsAtPath: 构建文件。fileWithData 也是一种 AVFile 文件构建方式。构建文件不会占用内存。最终都是使用 saveInBackgroundWithBlock 上传。