【发布时间】:2011-05-17 07:49:03
【问题描述】:
尝试从 XML 提要下载多个文件。有几个类别,每个类别都有未知数量的图像。我为每个类别创建一个子目录,没问题。当我尝试将每个文件下载到相应的类别时,每个文件都会为提要中的每个图像写入相同的数据。
我想我的问题是如何一次将 1 个文件下载到相应的目录而不用相同数据覆盖所有图像?
-(void)parsingComplete:(XMLDataSource*)theParser
{
/* iterate through the Categories and create the
sub-directory if it does not exist
*/
for (int i = 0; i < [categories count]; i++)
{
NSString *cat = [NSString stringWithFormat:@"%@/%@",BASE_DIR,[[categories objectAtIndex:i] objectForKey:@"name"]];
NSString *catName = [[categories objectAtIndex:i] objectForKey:@"name"];
NSArray *catArray = [[categories objectAtIndex:i] objectForKey:@"images"];
/* create the sub-direcotry naming it the #category# key */
if (![FILEMANAGER fileExistsAtPath:cat]) {
[FILEMANAGER createDirectoryAtPath:cat withIntermediateDirectories:NO attributes:nil error:nil];
}
//NSLog(@"\n\nCategory: %@",cat);
for (int x = 0; x < [catArray count]; x++)
{
/* download each file to the corresponding category sub-directory */
fileOut = [NSString stringWithFormat:@"%@/%@_0%i.jpg",cat,catName,x];
NSURLRequest * imageRequest =
[NSURLRequest requestWithURL:[NSURL URLWithString:[[catArray objectAtIndex:x] objectForKey:@"imageUrl"]]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
[[NSURLConnection alloc] initWithRequest:imageRequest delegate:self];
}
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[receivedData writeToFile:fileOut atomically:YES];
}
【问题讨论】:
标签: xml cocoa macos nsurlconnection nsurlrequest