【发布时间】:2016-02-26 02:07:18
【问题描述】:
我正在将多个图像和视频从服务器下载到手机磁盘,以实现无缝播放(流媒体不是一种选择)。对于图像,我只需将数据转换为UIImage。对于视频,我在NSTemporaryDirectory 中创建了一个包含视频数据的文件。然后将AVPlayerItem 与该文件关联。
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tempFile%i.mov",i]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Do background work
[fileManager createFileAtPath:filePath contents:videoData attributes:nil];
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:filePath];
AVAsset *asset = [AVAsset assetWithURL:outputURL];
AVPlayerItem *item = [[AVPlayerItem alloc]initWithAsset:asset];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:item, @"item", filePath, @"path", nil];
[self.pageImages replaceObjectAtIndex:i withObject:dic];
});
});
在每个视频结束时,我都会删除该文件。
NSDictionary *dic = [self.pageImages objectAtIndex:self.pageIndex];
if (dic) {
NSString *path = [dic valueForKey:@"path"];
if ([fileManager fileExistsAtPath:path]) {
[fileManager removeItemAtPath:path error:&error];
}
}
当视图消失时,我还会循环遍历 NSTemporaryDirectory
只是为了确保所有文件都已被删除。
NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
for (NSString *file in tmpDirectory) {
NSLog(@"CALLED TO DELETE");
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
}
当我第一次下载我的应用程序时,它只需要大约 5.0mb 的空间。但是在我观看了一系列视频和照片后,它的拍摄速度高达 >180.0mb。为什么即使在我删除了我创建的所有文件之后也会出现这种情况?
【问题讨论】:
-
您如何测量占用的空间?如果您在模拟器中,您实际上可以观看 tmp 目录。我还注意到您忽略(传递 NULL)作为删除调用的错误。可能真的有问题?
-
@BenZotto 我在删除之前和之后打印了 tempDirectory 数组。之前显示我的文件,之后显示一个 nil 数组,因此正在删除文件。我正在从我的 iPhone 设置中获取空间。设置-通用-存储&iCloud-管理存储
标签: ios nsfilemanager disk temporary-files