我正在尝试使用此代码同时从我自己的服务器下载几个文件:
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// create
[[NSFileManager defaultManager] createFileAtPath:strFilePath contents:nil attributes:nil];
_file = [NSFileHandle fileHandleForUpdatingAtPath:strFilePath];// read more about file handle
if (_file) {
[_file seekToEndOfFile];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedata
{
//write each data received
if( receivedata != nil){
if (_file) {
[_file seekToEndOfFile];
}
[_file writeData:receivedata];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//close file after finish getting data;
[_file closeFile];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//do something when downloading failed
}
- (IBAction)downloadFromServer:(id)sender {
NSLog(@"File now being downloaded");
while (i<=3) {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *strFileURL =[NSURL URLWithString:[NSString stringWithFormat:@"SomePath/pic%d.png", i]];
[request setURL:strFileURL];
NSURLConnection *conection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conection start];
strFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic%d.png", i]];
}
}
我有3张照片,pic1.png,pic2.png和pic3.png。现在,如果我运行此代码,应用程序将只保存一个名为pic3.png的损坏文件并自动崩溃。我需要下载所有三个文件,我出错的任何指针?
答案 0 :(得分:0)
很明显,这不起作用。您启动三个异步操作。每次启动异步操作后,您都会将文件名存储在未知位置,但可能位于相同位置。那么当第一个异步操作完成时将使用什么文件名?提示:不是您认为会使用的那个。
答案 1 :(得分:0)
不要使用简单的while循环来迭代REQUEST操作的数量。实际上Apple本身就有这么多内置库。所以我最好建议您尝试&#34; NSOPERATION QUEUE&#34; 。因此,您可以更好地查看Apple开发人员网站Notes,然后您就会明白:[1] https://developer.apple.com/library/IOs/documentation/Cocoa/Reference/NSOperationQueue_class/index.html
如何使用NSOperation处理不同的请求 - 如AddOperation或RemoveOperation,WaitUntilItFinishTheThread ..
答案 2 :(得分:0)
最好的方法是将所有图片放在一个zip文件中,下载并使用以下代码在真实设备上解压缩:
dispatch_queue_t queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:@"someDirectory/newPics.zip"];
NSError *error = nil;
// 2
NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
if(!error)
{
// 3
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *zipPath = [path stringByAppendingPathComponent:@"newPics.zip"];
[data writeToFile:zipPath options:0 error:&error];
if(!error)
{
ZipArchive *za = [[ZipArchive alloc] init];
// 1
if ([za UnzipOpenFile: zipPath]) {
// 2
BOOL ret = [za UnzipFileTo: path overWrite: YES];
if (NO == ret){} [za UnzipCloseFile];
// 3
NSString *imageFilePath = [path stringByAppendingPathComponent:@"newPics/pic1.png"];
//[self removeImage:zipPath];
[[NSFileManager defaultManager] removeItemAtPath:zipPath error: &error];
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *fileURL = [NSURL fileURLWithPath:imageFilePath];
[_webV loadRequest:[NSURLRequest requestWithURL:fileURL]];
});
}
}
else
{
NSLog(@"Error saving file %@",error);
}
}
else
{
NSLog(@"Error downloading zip file: %@", error);
}
});
这是最好的方法,快速可靠。