我正在尝试下载我的图像并存储到目录中。在用空格更新服务器端的图像名称之前,每件事都运行正常。我用以下代码处理空格。
NSString *newString = [getImageUrl stringByReplacingOccurrencesOfString:@" "
withString:@"%20"];
之后我得到了最终的URL字符串,如下所示
http://www.retail-king.com/image/data/men%20jeans/pd%201/5.jpg
当我在浏览器中打开此网址时,图像显示但我无法在应用中看到并下载带有此网址的图片。
我正在调用以下方法来下载图像。
-(void)downloadImageWithPath:(NSString *)_path andURL:(NSString *)_url
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:_url]];
NSLog(@"pic file = %@",_path);
NSLog(@"ASI http pic url:%@",[[request url] description]);
// [request setDownloadProgressDelegate:self];
[request setShowAccurateProgress:YES];
request.shouldContinueWhenAppEntersBackground=YES;
[request setDownloadDestinationPath:_path];
[request setTimeOutSeconds:30];
// [request setDelegate:self];
[request setStartedBlock:^{
NSLog(@"request started pic");
}];
[request setCompletionBlock:^{
// Use when fetching text data
NSLog(@"request completed pic");
currentPicsCount++;
if (currentPicsCount == picsCount && totalPicsDownload ) {
NSLog(@"All pics have downloaded");
totalPicsDownload = false;
[[SingltonClass getLoadingClassReference] dataSynced];
}
}];
[request setFailedBlock:^{
NSLog(@"request failed pic");
picsCount--;
}];
[request startAsynchronous];
} 调用setFailedBlock。其中显示pic无法下载。
答案 0 :(得分:0)
你的问题很模糊。我快速对URL进行了测试,并能够看到下载的图像。这可能不是一个理想的解决方案,但您可以了解正在发生的事情。我使用UIWebView
来显示图像,而棘手的部分可能是设置CGRectMake
的边界。你是如何下载和显示图像的?
如果有帮助,这是我的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
// Added space between keyword men and jeans
NSString *url = @"http://retail-king.com/image/data/men jeans/pd%201/5.jpg";
url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
// Check for URL
NSLog(@"Final Url = %@",url);
// Displaying it on webview
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSURL *targetURL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
// Program to check if the image is downloaded in directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *localFilePath = [paths objectAtIndex:0];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
[thedata writeToFile:localFilePath atomically:YES];
}
图片已成功下载 -
/Users/raurora/Library/Application Support/iPhone Simulator/7.1/Applications/554A5C94-1D29-42F0-886D-751CD3DFB155/Library/Caches/com.stackoverflow.doubt/fsCachedData/