AFNetworking 2 - 从NSString获取UIImage

时间:2014-10-24 17:51:16

标签: ios objective-c xml uiimage afnetworking

我得到了以下输入XML:

 <Company>
    <Id>1</Id>
    <IndustryId>1</IndustryId>
    <IndustriesID>
      <int>1</int>
      <int>2</int>
      <int>3</int>
    </IndustriesID>
    <Name>ClasOhlson</Name>
    <Icon>iVBORw0KGgoAAAANSUhEUgAAAKMAAAByCAYAAAA/DmEKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAZdEVYdFNvZnR3YXJlAEFkb2JlIEltYWdlUmVhZHlxyWU8AABdDklEQVR4Xu2dB5hURfb2W0GCRPOuuua0hjUnzIoYwQSIKEFEgkgUJCg

如何从UIImage代码获取<Icon>?我正在使用AFNetworking 2.0。我的代码:

NSString *str = [NSString stringWithFormat:@"%@/GetAllCompanies", FPHostAddress];
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

// Make sure to set the responseSerializer correctly
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSXMLParser *XMLParser = (NSXMLParser *)responseObject;
    [XMLParser setShouldProcessNamespaces:YES];
    id parser = [CompaniesXMLParser new];
    XMLParser.delegate = parser;
    BOOL isParseSuccessful = [XMLParser parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Download failed. Error: \n%@", error.localizedDescription);
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];

}];
[operation start];

1 个答案:

答案 0 :(得分:1)

如果要为iOS 7及更高版本创建应用程序目标,则应在从解析后的xml获取字符串后执行此操作:

NSData *data = [[NSData alloc] initWithBase64EncodedString:base64Encoded options:0];
UIImage* icon = [UIImage imageWithData: data];

否则,如果你的应用程序的基础iOS版本是iOS 7.0之前的版本,你应该从这里下载带有类别实现的文件:

http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

这样做:

NSData *data = [NSData dataFromBase64String:base64Encoded]; 
UIImage* icon = [UIImage imageWithData: data];

base64Encoded 是您从Icon标记中读取的内容。