我正在尝试下载base64编码的图像,它们发送一个带有xml标签的字节数组,图像大小可能从2到4 mb不等,iam将字节数组存储在一个字符串中然后解码它们,模拟器它工作正常,但当它进入设备时,应用程序崩溃完成图像下载。我不知道发生了什么,如果有人知道技术,请与我分享。
这是我的代码
// }
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[appDelegate hideActivityViewer];
NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
// [appDelegate hideActivityViewer];
//open an alert with just an OK button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription]
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[appDelegate hideActivityViewer];
self.receivedData = nil;
self.receivedData = nil;
[handle seekToFileOffset:100];
[handle readDataToEndOfFile];
NSString *aStr = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
NSLog(@"%@",aStr);
NSString *newString = [aStr stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
NSLog(@"%@", finString);
NSXMLParser *xmlPar=[[NSXMLParser alloc] initWithData: (NSData*)receivedData];
docDwnldPrsr *parser = [[docDwnldPrsr alloc] initXMLParser];
[xmlPar setDelegate:parser];
BOOL success = [xmlPar parse];
[aStr release];
self.receivedData = nil;
if (success){
if (self.rejectFlag == 1 || self.accptornot == 1) {
if ( [appDelegate.rejectResult isEqualToString:@"Successful"] && self.rejectFlag == 1) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"success" message:@"Aplication rejected successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
self.rejectFlag = 0;
appDelegate.rejectResult = @"";
}
if ([appDelegate.rejectResult isEqualToString:@"Successful"] && self.accptornot == 1 && self.sigNaturesent == 0) {
self.rejectFlag = 0;
sigNaturesent = 1;
NSMutableString *url = [[NSMutableString alloc] init];
[url appendString:urlip];
[url appendString:@"BSNL_CustomerSignUpload"];
NSURL *postUrl = [NSURL URLWithString:url];
MultipartForm *form = [[MultipartForm alloc] initWithURL:postUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
//[form addFormField:@"SecCode" withStringData:@"N9S8O7F6T5@I4N3D2I1A1"];
[form addFile:self.sigImagePath withFieldName:@"docbinaryarray"];
NSLog(@"%@", self.sigImagePath);
NSMutableURLRequest *postRequest = [form mpfRequest] ;
NSLog(urlip);
//[url release];
[urlip release];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
if (theConnection) {
self.receivedData=[NSMutableData data];
}
else {
NSLog(@"Failed");
}
[form release];
[theConnection release];
appDelegate.rejectResult = @"";
}
else if ([appDelegate.rejectResult isEqualToString:@"Successful"] && self.sigNaturesent == 1) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"success" message:@"Aplication accepted successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
self.sigNaturesent = 0;
}
}
else {
// decoding of images starts here
Byte inputData[[appDelegate.documentsImagesString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
[[appDelegate.documentsImagesString dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData];
size_t inputDataSize = (size_t)[appDelegate.documentsImagesString length];
size_t outputDataSize = EstimateBas64DecodedDataSize(inputDataSize);
Byte outputData[outputDataSize];
Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize);
NSData *theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize];
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:@"data.zip"];
NSLog(filePath);
appDelegate.documentsImagesString = nil;
NSFileManager *fm = [NSFileManager defaultManager];
[fm removeItemAtPath:[NSString stringWithFormat:@"%@/data",path] error:nil];
[theData writeToFile:filePath atomically:YES];
[theData release];
NSString *zip = [path stringByAppendingPathComponent:@"data"];
[fm createDirectoryAtPath:zip attributes:nil];
ZipArchive *unZip = [[ZipArchive alloc] init];
[unZip UnzipOpenFile:filePath];
[unZip UnzipFileTo:zip overWrite:YES];
[unZip UnzipCloseFile];
NSArray *arr = [fm subpathsAtPath:zip];
appDelegate.paths = arr;
NSMutableArray *temp = [[NSMutableArray alloc] init];
appDelegate.imagesData = temp;
[temp release];
[fm removeItemAtPath:filePath error:nil];
NSLog(@"the end");
appDelegate.path1 = zip;
// NSString *recData = [self decodeBase64:appDelegate.documentsImagesString];
// [recData writeToFile:@"/Users/madhukar/Desktop/data.txt" atomically:YES];
[appDelegate.imagesData addObject:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",zip, [arr objectAtIndex:1]]]];
[appDelegate hideActivityViewer];
self.docViewFlag = 1;
[self toggleDocumentsList:self];
}
}
}
请不要考虑评论 谢谢 库马尔