一个匿名的Imgur图片上传工具。
ImgurAnonymousAPIClient需要AFNetworking 2并且iOS 7.0或OS X 10.9。在iOS 8(以及可能OS X 10.10)上,需要AFNetworking 2.3.1或更高版本。
ImgurAnonymousAPIClient是灵活的
// Put your client ID in Info.plist first! Then…
NSURL *assetURL = info[UIImagePickerControllerReferenceURL];
[[ImgurAnonymousAPIClient client] uploadAssetWithURL:assetURL
filename:nil
completionHandler:^(NSURL *imgurURL, NSError *error) {
// imgurURL is ready for you!
// And the image was resized too, if needed.
// Even the largest images work fine!
}];
// Or…
UIImage *image = info[UIImagePickerControllerEditedImage];
[[ImgurAnonymousAPIClient client] uploadImage:image
withFilename:@"image.jpg"
completionHandler:^(NSURL *imgurURL, NSError *error) {
// imgurURL is ready for you!
// And the image was resized too, if needed.
}];
// Or…
NSURL *fileURLForSomeImage = ...;
[[ImgurAnonymousAPIClient client] uploadImageFile:fileURLForSomeImage
withFilename:nil
completionHandler:^(NSURL *imgurURL, NSError *error) {
// imgurURL is ready for you!
}];
// Or…
NSData *data = UIImageJPEGRepresentation(myImage, 0.9);
[[ImgurAnonymousAPIClient client] uploadImageData:data
withFilename:@"image.jpg"
completionHandler:^(NSURL *imgurURL, NSError *error) {
// imgurURL is ready for you!
}];
如果您使用CocoaPods,可以将它添加到您的Podfile
中
pod 'ImgurAnonymousAPIClient', :git => 'https://github.com/nolanw/ImgurAnonymousAPIClient.git', :tag => 'v0.1.1'
否则,客户端包含在ImgurAnonymousAPIClient.h
和ImgurAnonymousAPIClient.m
文件中。只需将这两个文件复制到您的项目中。如果您还没有安装,需要安装AFNetworking(版本2.2.2或更高版本)。最后,请确保链接到ImageIO
以及iOS上的AssetsLibrary
和MobileCoreServices
或MacOS上的CoreServices
。
一切设置完成后,您需要Imgur API客户端ID。这是使用Imgur API的必要条件,而ImgurAnonymousAPIClient正是使用该API。请确保注册您的应用程序并获得客户端ID。
指定客户端ID有三种方法。最方便的是将其放入您的Info.plist
中,键为ImgurAnonymousAPIClientID
// Uses client ID from Info.plist.
ImgurAnonymousAPIClient *client = [ImgurAnonymousAPIClient new];
// So does the convenient singleton.
[ImgurAnonymousAPIClient client];
或者创建一个客户端并赋予它客户端ID
[[ImgurAnonymousAPIClient alloc] initWithClientID:@"YOURIDHERE"];
或者在客户端创建后设置客户端ID
[ImgurAnonymousAPIClient client].clientID = @"YOURIDHERE";
ImgurSession是一个功能完整的Imgur API客户端。它支持完整的API,包括通过OAuth2登录。如果ImgurAnonymousAPIClient不能满足您的需求,它应该是您的首选。
在Test App文件夹中有一个iOS的功能示例应用(即外观不佳且不太易用的应用)。它展示了从照片库上传图片的几种不同方式,以及如何取消正在进行的上传。
上传到Imgur很方便,但处理所有可能的错误是一个巨大的痛苦。而且调整图片大小以适应最大文件大小也可能变得很复杂。ImgurAnonymousAPIClient将这些所有工作都封装起来。