iOS读取文件闪退

iOS读取文件闪退

问题

读取本地文件时出现内存暴涨,闪退等情况,报错信息如下

malloc: can't allocate region *** mach_vm_map(size=1404370944) failed (error code=3) UploadDemo(3736,0x1006cabc0) malloc: *** set a breakpoint in malloc_error_break to debug

读取问价你的代码如下

+ (NSData *)fileData:(NSString *)filePath {
    /**
     //闪退
     NSError *readError;
     NSFileHandle *fileHande = [NSFileHandle fileHandleForReadingFromURL:[NSURL URLWithString:filePath] error:&readError];
     if (!readError) {
     NSData *readData = [fileHande readDataToEndOfFile];
     [fileHande closeFile];
     return readData;
     }
     //闪退
     NSFileHandle *fileHande = [NSFileHandle fileHandleForReadingAtPath:filePath];
     return [fileHande readDataToEndOfFile];
     */
    //也闪退
    NSFileHandle *fileHande = [NSFileHandle fileHandleForReadingAtPath:filePath];
    return [fileHande availableData];
}

分析

纳闷为什么读大文件,尤其是视频文件的时候内存会暴涨的话,拍一下自己就明白了。

发现读取大文件的时候,尤其是视频文件时,App的内存暴涨?为什么会这样呢?

后来我想明白了,暴涨就对了,内存不涨那次有问题呢。文件在磁盘上存储着,当我们读取它的时候,会将读到的数据放到缓存里,手机的缓存是有限,但是文件很大,所以内存会暴涨,而且直接卡爆,然后报limit xx m的错误。

把文件从磁盘读出来,然后再存储到别的地方,这种思想就很可怕,读出来整个文件那得占用多大的缓存啊。这个把文件都读出来,然后再写入的思想本来就是不对的。

正确的做法应该是,循环读取,每次读小一部分数据,然后写入文件,直到把整个文件读完保存完。

解决

有种方式叫做”另存位“或者叫”文件拷贝“吧,先把文件拷贝到另一个地方,然后分片处理这些数据。

#pragma mark - 文件操作

int32_t const VH_CHUNK_SIZE = 8 * 1024;

//另存为
- (NSString *)writefile:(NSString *)filePath
{
    //写入文件路径
    NSString *toPath = [NSString stringWithFormat:@"%@/%@",NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject,[filePath lastPathComponent]];
    //如果存在,先删除
    if ([[NSFileManager defaultManager] fileExistsAtPath:toPath]) {
        [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil];
    }
    //创建文件路径
    if (![[NSFileManager defaultManager] createFileAtPath:toPath contents:nil attributes:nil]) {
        return nil;
    }
    //打开文件
    NSFileHandle *sourceHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
    NSFileHandle *writeHandle = [NSFileHandle fileHandleForWritingAtPath:toPath];
    if(sourceHandle == nil || writeHandle == nil) {
        return nil;
    }
    //读取文件写入
    BOOL done = NO;
    while(!done) {
        @autoreleasepool{
            NSData *data = [sourceHandle readDataOfLength:VH_CHUNK_SIZE];
            if([data length] == 0) {
                done = YES;
            }
            [writeHandle seekToEndOfFile];
            [writeHandle writeData:data];
            data = nil;
        }
    }
    //关闭文件
    [sourceHandle closeFile];
    [writeHandle closeFile];
    return toPath;
}
//删除本地上传的文件
- (BOOL)deletefile:(NSString *)uploadPath {
    if ([[NSFileManager defaultManager] fileExistsAtPath:uploadPath]) {
        BOOL removed = [[NSFileManager defaultManager] removeItemAtPath:uploadPath error:nil];
        return removed;
    }
    return NO;
}

结论

为什么要把整个文件都读出来,然后再去做别的事情,这种思想本身就陷入了一个坑。手机内存有限,不能一次性把较大的数据读出来。

发布了131 篇原创文章 · 获赞 9 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Morris_/article/details/102978063
今日推荐