Search code examples
iphoneobjective-ccocoa-touchios4

Search and save images from google images in iphone sdk


In my app I need to search images from google images for a particular word.

for example, i want to search image for Earth.

How to search images from Google Images and save selected images in documents directory?

Is there any API that can I use? Or any other way to do it?


Solution

  • Google API

    Google Image Search API / JSON Developer's Guide.

    You will have to display the images yourself as the result is a json response. However this will give you maximum control. The result will contain a thumbnail URL which you can use to show the preview.

    Downloading images

    Downloading images can be done in many ways:

    The use of the Google API does only work under certain circumstances such as

    "Applications that use this interface must abide by all existing Terms of Service. Most importantly, you must correctly identify yourself in your requests."

    Saving Images

    After receiving the results you can save the image to disk with:

    NSString *aFileName = @"myImage.jpg";
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentDirectory stringByAppendingPathComponent:aFileName];
    
    [myImageAsData writeToFile:path atomically:YES];
    

    alternatively if you've got the image as UIImage already:

    NSData *myImageAsData = [NSData dataWithData:UIImagePNGRepresentation(anImage)];
    [myImageAsData writeToFile:path atomically:YES];