Reading image metadata from web URL - iphone

Been looking around and can't find a definite way to do this...
I need to read the meta data from an image found at a certain URL. Is there a way to do this on the iphone?

Re-answer:
Try this: http://code.google.com/p/iphone-exif/
You should be able to fetch the image properties (metadata). Hope this helps :)

I tried using iphone-exif and it just wasn't working in my project. Saw a lot of people getting the same errors as me.
Good news is I found another solution...
NSDictionary *IPTCDict;
NSString *urlString = #"http://SOMEIMAGEURL.jpg";
NSMutableData *photoData = [NSMutableData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)photoData, NULL);
NSDictionary *metadata = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
NSLog(#"Meta: %#", metadata); //Will print out all the metadata
// Pulls out the IPTC data
IPTCDict = [metadata objectForKey:(NSString *)kCGImagePropertyIPTCDictionary];
NSLog(#"Caption: %#\nCopyright: %#", [IPTCDict objectForKey:#"Caption/Abstract"], [IPTCDict objectForKey:#"CopyrightNotice"]);

Related

iPhone App: how to get app icon from app id?

I am looking for a way to get the app icon from the app id. Do you know how to do it? Please share the way. Thanks.
e.g
Instagram, where the id I'm looking for is: id389801252
https://itunes.apple.com/jp/app/instagram/id389801252?mt=8
I want to get this image:
(I composed this answer after 2 minutes of googling... It's just the matter of the correct keyword!)
This is possible using an undocumented documented API of the iTunes Store. It might change in the future, but it doesn't seem to have changed in the near past, so here you are...
NSString *idString = #"id389801252";
NSString *numericIDStr = [idString substringFromIndex:2]; // #"389801252"
NSString *urlStr = [NSString stringWithFormat:#"http://itunes.apple.com/lookup?id=%#", numericIDStr];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *json = [NSData dataWithContentsOfURL:url];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSArray *results = [dict objectForKey:#"results"];
NSDictionary *result = [results objectAtIndex:0];
NSString *imageUrlStr = [result objectForKey:#"artworkUrl100"]; // or 512, or 60
NSURL *artworkURL = [NSURL URLWithString:imageUrlStr];
NSData *imageData = [NSData dataWithContentsOfURL:artworkURL];
UIImage *artworkImage = [UIImage imageWithData:imageData];
Note that this performs two synchronous round-trips using the NSURL API, so you better wrap this in a backgorund thread for maximal user experience. Feed this program an ID string (idString in the code above) and in the end, artworkImage will contain a UIImage with the desired image.
Just for reference, you can use the app's bundle id too:
http://itunes.apple.com/lookup?bundleId=com.burbn.instagram
Not sure if this is at all relevant anymore, but Apple provides an iTunes Link Maker tool. If you use this tool to find your app, you'll also see where it shows an App Icon section. Click embed and grab the img link from there. One thing to note, I did end up playing with the url a bit to find the right size and format I needed (for instance you can get a jpg render instead of png or select an arbitrary size like 128x128)

Fetch XML of Distance Matrix API (google) on iPhone

I'm trying to fetch the XML data from a query to the api without success...
I'm doing this:
[...]
NSURL *googleAPIurl = [NSURL URLWithString:#"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=28.124822,-15.430006&destinations=28.126953,-15.429874|28.072056,-15.416574|28.103186,-15.417665|28.127916,-15.625403|28.099125,-15.418365|28.107740,-15.454050|28.050825,-15.454066|28.051640,-15.454104|28.101788,-15.423592|28.113750,-15.446980|28.098871,-15.420730|28.098217,-15.449371|28.083364,-15.418172&mode=driving&sensor=false"];
NSData *xmlData = [NSData dataWithContentsOfURL:googleAPIurl];
NSError *error;
GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
if (xmlDocument == nil)
{
NSLog(#"NIL XML");
}
[...]
I'm ALWAYS getting a nil XML. NSData is always nil. I don't know what is happening with this. If I use a url with one destination only it works, but not for more than one. Also, I'm using the same method to retrieve xml with google places api with no problems. This is driving me crazy...
Please point me in the right direction.
Thanks in advance.
I suggested replacing all of the '|' with '%7C'
Turns out this is the more proper method to cover all of these character encoding issues:
NSURL *googleAPIurl = [NSURL URLWithString:[distancesURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Convert NSData to JSON

I have a NSData object, i need to convert it a NSDictionary object.
NSData *data = ....;
Now i need to convert this to a NSDictionary, How can i do this programatically ?
note: After i save the NSData to the NSDictionary i should be able to access key value pairs of the NSDictionary.
I don't have a code to demonstrate my workings so far, I have only created the NSData object, and have no clue to continue :)
You can subclass MKNetworkOperation and override the responseJSON method with the following:
-(id) responseJSON
{
NSString *rawJSON;
id jsonValue = nil;
if ((rawJSON = [[NSString alloc] initWithData:[self responseData] encoding:NSUTF8StringEncoding]) != nil) {
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
if ((jsonValue = [jsonParser objectWithString:rawJSON]) == nil) {
NSLog(#"This string doesn't seem to be JSON: '%#'\nraw Data : '%s'", rawJSON, (char *)[[self responseData] bytes]);
return [self responseString];
}
}
else {
NSLog(#"This data doesn't seem to be an UTF8 encoded string: %#", [self responseData]);
return [self responseString];
}
return jsonValue;
}
Please check this link of stack overflow, I have already consumed the JSON services, it will help you a lot. All of the coding is there.
JSON Data Conversion
And here is the tutorial with sample project
JSON Parse Tutorial
Hope you would find it helpful
I highly recommend invoking SBJSON framework, it saved my time for many times, exactly finished my work and easily to use. You don't need to know the details of the conversion algorithm, just download and invoke it.
You might want to download it from here, then follow this tutorial to get your things done.

How to convert XML string to JSON using iPhone sdk

I am implementing a client based application. In that I have an xml string. I need to convert it to JSON format and send to the server. I have no idea on converting this. Can you guys please suggest me any documentation or idea to this?
Step #1: Read XML into NSDictionary: http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/
Step #2: Convert NSDictionary into JSON: http://code.google.com/p/json-framework/
As Steve said the two steps are those, I leave you a bit of code, maybe can help you a bit more:
// Don't forget the imports ;)
#import "XMLReader.h"
// You must have a XML string from somewhere
NSString XMLString = yourXML;
// I remove all returns and tabs from the text, after i would be annoying if you don't remove it
XMLString = [XMLString stringByReplacingOccurrencesOfString:#"\r" withString:#""];
XMLString = [XMLString stringByReplacingOccurrencesOfString:#"\t" withString:#""];
// Parse the XML into a dictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:XMLString error:&parseError];
NSError *error;
self.dataParsed = [NSJSONSerialization dataWithJSONObject:xmlDictionary
options: NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
// Print the dictionary
NSLog(#"%#", xmlDictionary);

Extracting specific part of JSON respose on iPhone

I'm looking at adding a distance calculator to my application. I have been looking at Google's API put i cant seem to decode the JSON. I have managed to do so with PHP. The code for that was:
substr($convertedtoarray['routes']['0']['legs']['0']['distance']['text'], 0, -3);
On the iPhone i managed to get the JSON response but can't get the specific part of it that I want.
Json address: http://maps.googleapis.com/maps/api/directions/json?origin=plymouth&destination=pl210bp&sensor=false
NSMutableDictionary *luckyNumbers = [responseString JSONValue];
[responseString release];
if (luckyNumbers != nil) {
NSString *responseStatus = [luckyNumbers objectForKey:#"routes"];'
}
Where would I go from here?
Any help would be great cheers
NSString *responseStatus = [[[[[[luckyNumbers objectForKey:#"routes"]objectAtIndex:0] objectForKey:#"legs"]objectAtIndex:0] objectForKey:#"distance"] objectForKey:#"text"];
Very ugly you can extract in separate objects like this:
NSArray *routesArray = [luckyNumbers objectForKey:#"routes"];
NSDictionary *firstRoute = [routesArray objectAtIndex:0];
NSArray *legsArray = [firstRoute objectForKey:#"legs"];
NSDictionary *firstLeg = [legsArray objectAtIndex:0];
NSDictionary *distanceDict = [firstLeg objectForKey:#"distance"];
NSString *distanceText = [distanceDict objectForKey:#"text"];
Good luck.
The easiest thing to do would be to create a dictionary iterator, and loop over what children the luckynumbers dictionary has, you can print out, or debug, to see what the keys for these children are, and what object types they are.
I used this technique a few times to figure out what the structure of an XML doc I was being returned was like.