Giphy-iOS 是 iOS 中 Objective-C 的 Giphy API 客户端。
要运行示例项目,首先克隆仓库,然后在 Example 目录中运行 pod install
。
您应该在此处了解 Giphy 的 访问和 API 密钥。
'AXCGiphy' 提供了方便的访问 Giphy API 端点
您可以通过基于块的接口查询端点
- (void)viewDidLoad
{
[super viewDidLoad];
// set your API key before making any requests. You may use kGiphyPublicAPIKey for development.
[AXCGiphy setGiphyAPIKey:kGiphyPublicAPIKey];
[AXCGiphy searchGiphyWithTerm:@"frogs" limit:10 offset:0 completion:^(NSArray *results, NSError *error) {
self.giphyResults = results;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self.collectionView reloadData];
}];
}];
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AXCCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentifier forIndexPath:indexPath];
AXCGiphy * gif = self.giphyResults[indexPath.item];
NSURLRequest * request = [NSURLRequest requestWithURL:gif.originalImage.url];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
UIImage * image = [UIImage imageWithData:data];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
cell.imageView.image = image;
}];
}] resume];
return cell;
}
AXCGiphy 基于块的类方法提供了对 AXCGiphy 实例或 AXCGiphy 实例的 NSArray 的异步访问。AXCGiphy 实例表示 Giphy 的 GIF 和其元数据。然而,AXCGiphy 仅提供 GIF 的 URL。您如何使用这些 URL 取决于您自己。
我的示例使用 NSURLRequests 与 NSURLSession 从 URL 获取图像数据,并使用 mattt 的 AnimatedGifSerialization 将动画 GIF 解码为动画 UIImages。如果您不确定如何进行操作,请以我的示例应用程序作为起点。
基于块的类方法返回 URLSessionDataTasks 以提供额外的控制,如果您需要的话。
AXCGiphy 还提供了用于这些端点的类方法以生成 NSURLRequests。
AFNetworking/Serialization 2.3.1
Alex Choi,[email protected]
Giphy-iOS 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。