无法将NSData转换为图像iphone [英] Cannot convert NSData to Image iphone

查看:137
本文介绍了无法将NSData转换为图像iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道为什么它不会将 NSData 转换为 UIImage NSData 得到保存,但是当我调试程序时,想象是0X0000000000当转换与imageData。

I really don't know why it doesn't convert the NSData to UIImage. The NSData gets saved, but when I debug the program, imagine is 0X0000000000 when converting with imageData.

这是我的代码:

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 我按下一个按钮

The code where I parse the data from the web service. I call writeDataToFileSystem my pressing a button

- (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]);
}


推荐答案

imageWithData:不接受一些任意数据,并将其解释为RGBA未压缩纹理。

imageWithData: doesn't take some arbitrary data and interpret it as an RGBA uncompressed texture.

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

It takes a data that contains bytes of a recognized image format (like JPG, PNG, or TIFF), parses those and decompresses the images appropriately.

这篇关于无法将NSData转换为图像iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆