0

我正在开发一个正在下载一系列 zip 文件的应用程序。

它工作正常,但有时它会崩溃,我可以在项目导航器中看到以下错误:

Thread 5 WebThread: EXC_??? (11)(code=0,subcode=0x0)

有任何想法吗?我正在使用ASIHTTPRequest下载文件。

4

2 回答 2

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.

于 2013-02-20T10:43:01.763 回答
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

}

希望能帮助到你 !!!

于 2013-02-20T09:46:52.207 回答