无法从ASIHTTPRequest中的downloadDestination路径访问图像

时间:2011-01-11 08:01:12

标签: iphone objective-c iphone-sdk-3.0

我正在将图像下载到Iphone HomeDirectory中的specfic文件夹,但无法访问它,因为它以随机生成的名称保存图像。所以请帮助我如何在我的其他功能中正确访问它们。

 - (void)fetchURL:(NSURL *)url
{
 request1 = [ASIHTTPRequest requestWithURL:url];
 NSString *str1 = [NSString stringWithFormat:@"savedImage%d",i1];
 NSString *path = [self.documentsDirectoryPath stringByAppendingPathComponent:str1];
 NSLog(@"saved image path %@",path);
 [request1 setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
  //path];
 //[request1 setTemporaryFileDownloadPath:[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"1"]];
 [request1 setAllowResumeForFileDownloads:YES];
 [request1 setDelegate:self];
 [request1 setDidFinishSelector:@selector(URLFetchWithProgressComplete:)];
 [request1 setDidFailSelector:@selector(URLFetchWithProgressFailed:)];
 [request1 setDownloadDestinationPath:[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request1]];
 [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO]; 
 [request1 startAsynchronous];

 i1++;

 //[request1 setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
 //[request setDownloadProgressDelegate:imageProgressIndicator1];
 //[self.networkQueue addOperation:request1];
}

从其他功能访问图像

    ASIDownloadCache *cache = [[[ASIDownloadCache alloc] init] autorelease];

 //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES); 
 self.documentsDirectoryPath = NSHomeDirectory();
 //[paths objectAtIndex:0];

 [cache setStoragePath:self.documentsDirectoryPath];

NSLog(@"path %@",self.documentsDirectoryPath);

  self.destinationPath = [documentsDirectoryPath stringByAppendingFormat:@"/Library/Caches/ASIHTTPRequestCache/SessionStore"];

  //NSString *PathStr = [NSString stringWithFormat:@"savedImage%d",i];
  //NSString *appFile = [self.destinationPath stringByAppendingPathComponent:PathStr];

  //NSString *path = [self.documentsDirectoryPath stringByAppendingPathComponent:PathStr];
  //NSLog(@"images path %@",path);

  NSLog(@"Download Destination Path %@",[request1 downloadDestinationPath]);
  imageView.image = [UIImage imageWithContentsOfFile:[request1 downloadDestinationPath]];
         //self.destinationPath];
         //[request1 downloadDestinationPath]];

  NSLog(@"Image %@ and %d",imageView.image,i);

2 个答案:

答案 0 :(得分:1)

为什么要设置两次下载路径?

 [request1 setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
  ...
 [request1 setDownloadDestinationPath:[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request1]];

为什么要设置下载到缓存的请求?你应该只有一个downloadDestinationPath。

答案 1 :(得分:0)

看起来您有两次拨打setDownloadDestinationPath。第一个设置主目录子目录的路径,但第二个路径更改它(如documentation的缓存部分所述)。您可以保存到任何一个地方,但不能同时保存到两个地方。