NSData
を に変換しない理由が本当にわかりませんUIImage
。はNSData
保存されますが、プログラムをデバッグすると、imageDataで変換すると0X0000000000になると想像してください。
これは私のコードです:
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [[pathArr objectAtIndex:0]
stringByAppendingPathComponent:@"img.data" ];
NSData * imageData = [cerereIndemnizatie.fisier dataUsingEncoding: NSUTF8StringEncoding];
[imageData writeToFile:path atomically:YES];
NSData *retrievedData = [NSData dataWithContentsOfFile:path];
UIImage * imagine = [UIImage alloc] init];
UIImage * imagine = [UIImage imageWithData:retrievedData];
Web サービスからのデータを解析するコード。writeDataToFileSystem
私はボタンを押すと呼びます
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL * url = [NSURL URLWithString:@"http://192.168.1.183:8002/iDocument.asmx"];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:15.0];
NSString * parameters = [[NSString alloc] initWithString:[self XmlRequest]];
NSLog(@"Request=%@", parameters);
NSString * msgLength = [NSString stringWithFormat:@"%d", [parameters length]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://tempuri.org/GetDocumenteByIdXML" forHTTPHeaderField:@"SOAPAction"];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
if (connection){
self.responseData = [NSMutableData data];
}
}
- (NSString *)XmlRequest
{
NSString * xml = [[NSString alloc] initWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
"<soap:Body>"
"<GetDocumenteByIdXML xmlns='http://tempuri.org/'>"
"<user>admin_document</user>"
"<password>123</password>"
"<id>%@</id>"
"</GetDocumenteByIdXML>"
"</soap:Body>"
"</soap:Envelope>",self.index];
return xml;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Response: %@", [response textEncodingName]);
[self.responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"connection:DidReceiveData");
[self.responseData appendData:data];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSString * responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response string: %@", responseString);
self.navigationItem.hidesBackButton = NO;
//to do parseXML
NSXMLParser * xmlParser = [[NSXMLParser alloc] initWithData:responseData];
xmlParser.delegate = self;
BOOL succes = [xmlParser parse];
if (succes) {
[self.tableView reloadData];
}
else {
}
}
- (void) writeDataToFileSystem {
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [[pathArr objectAtIndex:0]
stringByAppendingPathComponent:@"img.data" ];
NSData * imageData = [cerereIndemnizatie.fisier dataUsingEncoding: NSUTF8StringEncoding];
[imageData writeToFile:path atomically:YES];
NSData *retrievedData = [NSData dataWithContentsOfFile:path];
//UIImage * imagine = [[UIImage alloc] init];
//UIImage * imagine = [UIImage imageWithData:retrievedData];
UIImage * imagine = [[UIImage alloc] initWithData:retrievedData];
// UIImageView *imgv = [[UIImageView alloc]
// initWithFrame:CGRectMake(100, 100, 200, 200)];
//
// imgv.image = [UIImage imageWithData:retrievedData];
// [self.view addSubview:imgv];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if (![elementName isEqual:@"DetaliiDocument"])
return;
cerereIndemnizatie = [[CerereIndemnizatieDoc alloc] init];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"Id"]) {
cerereIndemnizatie.id = currentElementValue;
}
if ([elementName isEqualToString:@"Nume"]) {
cerereIndemnizatie.nume = currentElementValue;
}
if ([elementName isEqualToString:@"TipDocument"]) {
cerereIndemnizatie.tipDocument = currentElementValue;
}
if ([elementName isEqualToString:@"Fisier"]) {
cerereIndemnizatie.fisier = currentElementValue;
}
else if ([elementName isEqualToString:@"FormatDocument"]) {
cerereIndemnizatie.formatDocument = currentElementValue;
}
currentElementValue = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"connection:didFailWithError:");
NSLog(@"%@", [error localizedDescription]);
}