Investor06
Investor06

Reputation: 27

Getting UIImage to work from JSON

I'm having an issue getting my UIImage to work from the JSon that I received. Here let's take a look at my JSON

[
{
node_title: "Fumi",
node_uid: "11",
users_node_name: "pae",
nid: "7",
Body: "<p>This is Fumi Restaurant</p> ",
Enterprise Address: "99/9",
Enterprise Email: "[email protected]",
Enterprise Tel: "08111111",
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fumi.jpg" width="300" height="300" alt="" />"
},
{
node_title: "Fuji",
node_uid: "8",
users_node_name: "testent",
nid: "1",
Body: "<p>Fuji</p> ",
Enterprise Address: "Somwhere it belong",
Enterprise Email: "[email protected]",
Enterprise Tel: "02-999-9999",
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fuji.png" width="320" height="320" alt="" />"
}
]

and Here is my code: I'm able to get all the Json that I received and show them on this ViewController. However, for the [self.detail valueForKey:@"Enterprise Logo"] doesn't seems to work due to the content of the JSON.

BTW, self.detail is the Json object that I passed from the previous UITableViewController by doing preparedtosegue.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.EnterPriseName.text = [self.detail valueForKey:@"node_title"];
    self.EnterPriseTel.text = [self.detail valueForKey:@"users_node_name"];
    self.EnterPriseBody.text = [self.detail valueForKey:@"Body"];
    self.EnterPriseEmail.text = [self.detail valueForKey:@"nid"];

    [self.picLogo setImageWithURL:[NSURL URLWithString:[self.detail valueForKey:@"Enterprise Logo"]]];

}

I also try this method which works just fine but the content in Enterprise Logo must contain only the URL of that img only. I'm trying to do things as dynamic as much as possible, so I don't expect the static answer. Is there a way for me to fix this issue ?

[self.picLogo setImageWithURL:[NSURL URLWithString:@"http://localhost/drupal/sites/default/files/Fumi.jpg"]];

Upvotes: 0

Views: 179

Answers (3)

Jonathan
Jonathan

Reputation: 2383

After retrieving the value of Enterprise Logo just extract the image URL and request the image using NSURLConnection, NSURLSession, or AFNetworking.

e.g.

NSString * __block imageURL = nil;
NSArray * objects = [[self.details valueForKey:@"Enterprise Logo"] componentsSeparatedByString:@" "];

[objects enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop)
{
    NSRange range = [object rangeOfString:@"src="];
    if (range.location != NSNotFound)
    {
        imageURL = [object substringFromIndex:range.location+range.length];
        *stop = YES;
    }
}];

Upvotes: 0

abmussani
abmussani

Reputation: 461

Extract src value from img tag of Enterprise Logo that will be the url for image.

To extract the Url, please look into this link: retrive image url from string

Let me know if you have any issue :)

Upvotes: 0

Arun
Arun

Reputation: 117

Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fuji.png" width="320" height="320" alt="" />"

The vale for the Enterprise Logo key is html in your json.

[self.picLogo setImageWithURL:[NSURL URLWithString:[self.detail valueForKey:@"Enterprise Logo"]]];

The code you are using is looking for a URL to that image. not the html.

Best practice would be to just have the json have the image url in the format.

http://localhost/drupal/sites/default/files/Fuji.png

and not the html in it.

Upvotes: 2

Related Questions