【发布时间】:2016-11-15 23:27:59
【问题描述】:
我想下载压缩包并解压。当我下载 zip 时,一切正常。但是当解压缩时,我的界面会停止几秒钟,有时应用程序会崩溃。如何解决?
压缩文件大小 650 MB
downloadButton - 点击按钮激活下载
-(IBAction) downloadButton:(id)sender{
_url1 =[NSURL URLWithString:@"link"];
_downloadTask1 = [_session downloadTaskWithURL:_url1];
[_downloadTask1 resume];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
if (downloadTask == _downloadTask1) {
_paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
_documentsDirectory1 = [_paths1 objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *newLocation = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/1.zip", _documentsDirectory1]];
NSError *error;
[fileManager copyItemAtURL:location toURL:newLocation error:&error];
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;{
if( downloadTask == _downloadTask1){
_progress1 = [[NSNumber numberWithInteger:totalBytesWritten] floatValue];_total1 = [[NSNumber numberWithInteger:totalBytesExpectedToWrite] floatValue];
_percentage = [NSString stringWithFormat:@"%.f%%", ((_progress1 / _total1) * 100)];
(NSLog (_percentage, @"%.f%%"));
_label.text = _percentage;
}
if ([_percentage isEqual: @"100%"]) {
_label.text = @«ok»;
[self performSelector:@selector(zip) withObject:nil afterDelay:0.1];
[self performSelector:@selector(removeZip) withObject:nil afterDelay:5.0];
}
}
-(void) zip{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
_documentsDirectory1 = [_paths1 objectAtIndex:0];
_zipPath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"1.zip"];
_destinationPath1 = [NSString stringWithFormat:@"%@",_documentsDirectory1];
[SSZipArchive unzipFileAtPath:_zipPath1 toDestination:_destinationPath1];
});
}
-(void) removeZip{
_paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
_documentsDirectory1 = [_paths1 objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:[_documentsDirectory1 stringByAppendingPathComponent:@"1.zip"] error:nil];
}
【问题讨论】:
-
如果崩溃,控制台是否有任何错误信息?你必须在主线程上解压缩吗?
-
1.我检查控制台。没有消息。应用程序只是折叠。 2.如果我不使用dispatch_async app fading。如果我使用 dispatch_async 应用程序滞后
标签: ios objective-c zip