1

我真的不知道为什么它不转换NSDataUIImage. 被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]);
}
4

2 回答 2

0

imageWithData:不会获取一些任意数据并将其解释为 RGBA 未压缩纹理。

它获取包含已识别图像格式(如 JPG、PNG 或 TIFF)字节的数据,解析这些字节并适当地解压缩图像。

于 2012-11-16T11:23:30.243 回答
0

图像在后端以某种方式(例如base64)编码,尝试发现使用哪种编码并在前端使用相同的算法对图像进行解码。NSUTF8StringEncoding 在这种情况下没有意义。

于 2012-11-16T12:28:38.017 回答