NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"SavedImages"];
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *imageName = [NSString stringWithFormat:@"/savedImage1.jpg"];
NSString *fileName = [stringPath stringByAppendingString:imageName];
NSURL *url = [NSURL URLWithString:@"http://example.com/pictures/sample
.JPG“]; ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:[NSString stringWithFormat:@"%@", stringPath]]; //use the path from earlier
[request setDelegate:self];
[request startAsynchronous];
[request setCompletionBlock:^
{
NSData * dataFromrequest = [request responseData];
NSLog(@"request data = %@",dataFromrequest);
dataFromrequest writeToFile:fileName atomically:YES];
}];
我正在使用此代码从网址下载图片。但我使用的是另一个网址,而不是“http://example.com/pictures/sample.jpg”,有效的网址。当我尝试这个代码..它没有去完成块。请告诉我问题是什么,我做错了什么。
谢谢
答案 0 :(得分:1)
一切看起来都不错,对于完成块我们不想使用委托。请按照代码进行操作。
ASIHTTPRequest * photoRequest = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:“http://example.com/pictures/sample.jpg”)]];
[photoRequest setDownloadDestinationPath:[NSString stringWithFormat:@“%@ / sample.png”,stringPath]];
[photoRequest setCompletionBlock:^ {
NSLog(@"Response %d", [photoRequest responseStatusCode]);
}];
[photoRequest startAsynchronous];
希望您理解代码。