我正在开发一个下载一系列zip文件的应用程序。
它工作正常,但有时会崩溃,我可以在项目导航器中看到以下错误:
Thread 5 WebThread: EXC_??? (11)(code=0,subcode=0x0)
有什么想法吗?我正在使用ASIHTTPRequest
下载文件。
答案 0 :(得分:0)
除了使用ASIHTTPRequest之外,您还可以尝试异步请求。以下代码为您提供有关下载图像的详细信息,只需将其替换为您的zip url链接
即可这是下载选项的另一种方法。在应用程序中使用更多线程功能将导致性能问题。
-(IBAction)startdownload
{
for (int i=0; i<[downloadarray count]; i++) //download array have url links
{
NSURL *URL = [NSURL URLWithString:[downloadarray objectAtIndex:i]];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:URL];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if([data length] > 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
{
//make your image here from data.
UIImage *imag = [[UIImage alloc] initWithData:[NSData dataWithData:data]];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [array objectAtIndex:0];
NSString *imgstr=[NSString stringWithFormat:@"%d",i];
NSString *pngfilepath = [NSString stringWithFormat:@"%@sample%@.png",docDir,imgstr];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(imag)];
[data1 writeToFile:pngfilepath atomically:YES];
}
else if ([data length] == 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
{
NSLog(@"No Data!");
}
else if (![[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"]){
NSLog(@"Error = %@", error);
}
}];
}
-(IBAction)viewfile
{
NSMutableArray *arr=[[NSMutableArray alloc]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pngfilepath = [NSString stringWithFormat:@"%@",documentsDirectory];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:pngfilepath error:&error];
for (int i=0; i<[filePathsArray count]; i++){
NSString *pngfilepath = [NSString stringWithFormat:@"%@%@",documentsDirectory, [filePathsArray objectAtIndex:i]];
[arr addObject:[UIImage imageWithContentsOfFile:pngfilepath]];
}
myimageview = [[UIImageView alloc] initWithImage:[arr objectAtIndex:0]]; //Here myimageview is UIImageView
}
希望它能帮助!!!
答案 1 :(得分:0)
您可以跟踪异常以获取更多详细信息吗?
用以下代码替换你的main.m:
int main(int argc, char *argv[])
{
@autoreleasepool {
int retVal = -1;
@try {
retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([FRAppDelegate class]));
}
@catch (NSException* exception) {
NSLog(@"Uncaught exception: %@", exception.description);
NSLog(@"Stack trace: %@", [exception callStackSymbols]);
}
return retVal;
}
}
我也使用ASIHTTPRequest
进行下载,对我来说效果很好。我正在使用ASINetworkQueue
。