I am trying to download images which are base64 encoded, they are sending an byte array apended with xml tags, the images size may vary from 2 to 4 mb, iam storing the byte array in a string and then decoding them, in simulator it works fine but when it goes to device, app crashes when download of images completed. I am not getting what happened, if any one knows the techinique please share with me.
Here is my code
// }
- (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];
}
}
}
please dont consider comments thanks Kumar
精彩评论