Search and save images from Google images in iphone sdk

In my application, I need to search for images from Google images for a specific word.

For example, I want to find an image for Earth .

How to search images from Google images and save selected images in the document directory?

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

+6
source share
3 answers

Google API

Google / JSON Image Search API Guide .

You will need to display the images yourself, resulting in a json response. However, this will give you maximum control. The result will contain a sketch that you can use to display the preview.

Upload Images

Image loading can be done in various ways:

Using the Google APIs only works under certain circumstances, such as

“Applications that use this interface must adhere to 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 using:

 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]; 

if you already have a UIImage image:

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

First you need the Google Image Search API. http://code.google.com/apis/imagesearch/

Further, if you are only happy with targeting on iOS 4.0 and later, you can use Grand Central Dispatch and the very useful sycnhronous APIs included with iOS. In particular, I am thinking of [NSData dataWithContentsOfURL:]. This will block the thread until it loads the entire image, so it does not start it in the main thread . Fortunately, GCD allows you to easily throw material onto a background stream. Therefore, your code will probably look something like this:

 dispatch_queue_t NetworkQueue = dispatch_queue_create("NetworkQueue", 0); dispatch_async(NetworkQueue,^{ NSData* imageResultsData = [NSData dataWithContentsOfURL: [NSURL urlWithString:@"https://ajax.googleapis.com/ajax/services/search/images?some_images_query_here"]; //do something with that data //let say you unpacked it into an NSArray of NSURLs for(NSURL* url in myImageArray) { dispatch_async(NetworkQueue,^{ NSData* imageData = [NSData dataWithContentsOfURL:url]; [imageData writeToFile:file_name_here atomically:YES]; }); } }); 
+3
source

Google Image Search does not work:

"The Google Image Search API has officially been deprecated since May 26, 2011. It will continue to work according to our deprecation policy, but the number of requests you can make per day may be limited. We recommend using the Custom Search API, which now supports image search." .

Here is a link to the custom search API

0
source

Source: https://habr.com/ru/post/888897/


All Articles