I'd like to download images from a network path. But since there is a risk of big files, I thought it would be a good idea to write/append partial NSdata chunks directly to a file in documents directory. Is this posible? I have an asynchronous URLConnection set up. It calls back to a delegate
我想从网络路径下载图片。但是由于存在大文件的风险,我认为将部分NSdata块直接写入/追加到documents目录中是个好主意。这是可能吗?我设置了一个异步URLConnection。它调用一个委托
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
method. In this method, I'd like to append the so-far-downloaded "data" to a file directly to the disk. Can you help a bit? If this would be posibble without low level C, it would be perfect.
方法。在这个方法中,我希望将到目前为止下载的“数据”直接附加到磁盘上的文件中。你能帮点忙吗?如果这是一个没有低C的正位,那就太完美了。
1 个解决方案
#1
7
I havent tried this ever, but from this apple docs, I'd suggest
我从来没有尝试过这个,但我建议从这个苹果文档开始
@property (nonatomic,strong) NSFileHandle *handle;
//somewhere in a init or viewDidLoad
self.handle = [NSFileHandle fileHandleForUpdatingAtPath:filePath]
-(void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[handle seekToEndOfFile];
[handle writeData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self.handle closeFile];
}
Note: the filePath must be a valid path for an existing file, there are numerous posts dealing with creating a file.
注意:文件路径必须是现有文件的有效路径,有很多文章都是关于创建文件的。
#1
7
I havent tried this ever, but from this apple docs, I'd suggest
我从来没有尝试过这个,但我建议从这个苹果文档开始
@property (nonatomic,strong) NSFileHandle *handle;
//somewhere in a init or viewDidLoad
self.handle = [NSFileHandle fileHandleForUpdatingAtPath:filePath]
-(void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[handle seekToEndOfFile];
[handle writeData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self.handle closeFile];
}
Note: the filePath must be a valid path for an existing file, there are numerous posts dealing with creating a file.
注意:文件路径必须是现有文件的有效路径,有很多文章都是关于创建文件的。