gpt4 book ai didi

ios - 无法将 NSData 转换为图片 iphone

转载 作者:行者123 更新时间:2023-12-03 17:23:38 28 4
gpt4 key购买 nike

我真的不知道为什么它不将 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]);
}

最佳答案

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

关于ios - 无法将 NSData 转换为图片 iphone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13415505/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com