在 iphone sdk 中搜索并保存来自 google images 的图像

发布于 2024-11-09 13:10:12 字数 150 浏览 0 评论 0 原文

在我的应用程序中,我需要从谷歌图像中搜索图像中的特定单词。

例如,我想在图像中搜索地球

如何从 Google 图片搜索图像并将选定的图像保存在文档目录中?

有什么 API 可以使用吗?或者有其他方法可以做到吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖阳 2024-11-16 13:10:12

Google API

Google 图片搜索 API/JSON 开发人员指南

您必须自己显示图像,因为结果是 json 响应。然而,这将为您提供最大程度的控制。结果将包含一个缩略图 URL,您可以使用它来显示预览。

下载图像

可以通过多种方式下载图像:

Google 的使用API 仅在某些情况下有效,例如

“使用此接口的应用程序必须遵守所有现有的服务条款。最重要的是,您必须在请求中正确识别自己的身份。”

保存图像

收到结果后,您可以将图像保存到磁盘:

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

或者,如果您已经将图像保存为 UIImage :

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

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];
不知所踪 2024-11-16 13:10:12

首先,您需要 Google 图片搜索 API。 http://code.google.com/apis/imagesearch/

接下来,如果您仅针对 iOS 4.0 及更高版本很酷,您可以使用 Grand Central Dispatch 和 iOS 中包含的非常有用的同步数据 API。具体来说,我正在考虑 [NSData dataWithContentsOfURL:]。这将阻塞线程,直到它下载整个图像,因此不要在主线程上运行它。幸运的是,GCD 可以让您轻松地将东西扔到后台线程上。所以你的代码可能看起来像这样:

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's 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];
        });
   }
});

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

Next, if you're cool with only targeting iOS 4.0 and later, you can use Grand Central Dispatch and the very useful sycnhronous data APIs included in iOS. Specifically, I'm thinking of [NSData dataWithContentsOfURL:]. This will block the thread until it downloads the whole image, so don't run it on the main thread. Fortunately, GCD lets you throw stuff on a background thread with ease. So 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's 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];
        });
   }
});
浅蓝的眸勾画不出的柔情 2024-11-16 13:10:12

Google 图片搜索无法运行:

“Google 图片搜索 API 已于 5 月份正式弃用
2011 年 2 月 26 日。它将继续按照我们的弃用政策工作,但是
您每天可以提出的请求数量可能会受到限制。我们
鼓励您使用自定义搜索 API,它现在支持图像
搜索。”

这是自定义搜索 API 的链接

Google Image Search is not working:

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

Here is the link to the Custom Search API

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文