How to download a large file with the iPhone SDK and avoid memory usage issues?

前端 未结 3 1873
半阙折子戏
半阙折子戏 2020-12-04 11:26

I\'m using the NSURLConnection class to download a large file in my iPhone application, but it crashes every so often because it\'s using too much memory. I\'m

3条回答
  •  一向
    一向 (楼主)
    2020-12-04 12:24

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
    
        filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:save_name]; // filename is in .h file
    
        [[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
            file =
    [[NSFileHandle fileHandleForUpdatingAtPath:filename] retain];// file is in .h 
    
    //if (file)     {
    //
    //      [file seekToEndOfFile];
    //  }
     }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSD
    ata *)data {
    
     if (file)  { 
    
            [file seekToEndOfFile];
    
        } [file writeData:data]; 
    
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection*)connection { 
    
    [file closeFile]; 
    
    }
    

提交回复
热议问题