Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kcharwood committed Apr 30, 2012
1 parent 5b24fec commit bdd60cc
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ build/*
xcuserdata
profile
*.moved-aside

.DS_Store
117 changes: 117 additions & 0 deletions KHGravatar/UIImageView+KHGravater.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// UIImageView+KHGravatar.h
//
// Copyright (c) 2012 Kevin Harwood
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <UIKit/UIKit.h>

typedef enum {
KHGravatarDefaultImageDefault = 0,
KHGravatarDefaultImage404,
KHGravatarDefaultImageMysteryMan,
KHGravatarDefaultImageIdenticon,
KHGravatarDefaultImageMonsterId,
KHGravatarDefaultImageWavatar,
KHGravatarDefaultImageRetro,
}KHGravatarDefaultImage;

typedef enum {
KHGravatarRatingG = 0,
KHGravatarRatingPG,
KHGravatarRatingR,
KHGravatarRatingX,
}KHGravatarRating;

/**
This category adds methods to the UIKit framework's `UIImageView` class to automatically download images from Gravatar for a specified email address. The methods in this category provide support for loading remote images asynchronously from a URL using the `AFNetworking` UIImageView category. This class requires `AFNetworking` to already be included in the project.
Note that all methods return the properly sized image based on the size of the UIImageView and the scale of the screen.
*/
@interface UIImageView (KHGravater)

/**
Creates and enqueues an image request operation, which asynchronously downloads the Gravatar image for the specified email address, and sets it the request is finished. If the image is cached locally, the image is set immediately, otherwise, the image is set once the request is finished.
@discussion By default, url requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set to use HTTP pipelining, and not handle cookies. To configure url requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
@param emailAddress The email address used for the Gravatar image request.
*/
- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress;

/**
Creates and enqueues an image request operation, which asynchronously downloads the Gravatar image for the specified email address. If the image is cached locally, the image is set immediately. Otherwise, the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished.
@param emailAddress The email address used for the Gravatar image request.
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
@discussion By default, url requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set to use HTTP pipelining, and not handle cookies. To configure url requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
*/
- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress placeholderImage:(UIImage*)placeholderImage;

/**
Creates and enqueues an image request operation, which asynchronously downloads the Gravatar image for the specified email address. If the image is cached locally, the image is set immediately. Otherwise, the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. If the email address does not have a Gravatar, the default image will be returned as specified in the defaultImageType.
@param emailAddress The email address used for the Gravatar image request.
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
@param defaultImageType The type of image returned if no Gravatar exists for the specified email address.
@param rating The acceptable rating for the image to be used within your application.
@discussion By default, url requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set to use HTTP pipelining, and not handle cookies. To configure url requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
*/
- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress
placeholderImage:(UIImage*)placeholderImage
defaultImageType:(KHGravatarDefaultImage)defaultImageType
rating:(KHGravatarRating)rating;

/**
Creates and enqueues an image request operation, which asynchronously downloads the Gravatar image for the specified email address. If the image is cached locally, the image is set immediately. Otherwise, the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. If the email address does not have a Gravatar, the default image will be returned as specified by the defaultImageURL.
@param emailAddress The email address used for the Gravatar image request.
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
@param defaultImageURL The URL pointing to the image to return if no Gravatar is found for the specified email address.
@param rating The acceptable rating for the image to be used within your application.
@discussion By default, url requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set to use HTTP pipelining, and not handle cookies. To configure url requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
*/
- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress
placeholderImage:(UIImage*)placeholderImage
defaultImageURL:(NSURL*)defaultImageURL
rating:(KHGravatarRating)rating;

/**
Creates and enqueues an image request operation, which asynchronously downloads the Gravatar image for the specified email address. If the image is cached locally, the image is set immediately. Otherwise, the specified placeholder image will be set immediately, and then the remote image will be set once the request is finished. If the email address does not have a Gravatar, the default image will be returned as specified by the defaultImageURL.
@param emailAddress The email address used for the Gravatar image request.
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes.
@param defaultImageType The type of image returned if no Gravatar exists for the specified email address.
@param rating The acceptable rating for the image to be used within your application.
@param success A block to be executed when the image request operation finishes successfully, with a status code in the 2xx range, and with an acceptable content type (e.g. `image/png`). This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the request and response parameters will be `nil`.
@param failure A block object to be executed when the image request operation finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred.
@discussion By default, url requests have a cache policy of `NSURLCacheStorageAllowed` and a timeout interval of 30 seconds, and are set to use HTTP pipelining, and not handle cookies. To configure url requests differently, use `setImageWithURLRequest:placeholderImage:success:failure:`
*/
- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress
placeholderImage:(UIImage *)placeholderImage
defaultImageType:(KHGravatarDefaultImage)defaultImageType
rating:(KHGravatarRating)rating
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;

@end
180 changes: 180 additions & 0 deletions KHGravatar/UIImageView+KHGravater.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// UIImageView+KHGravatar.h
//
// Copyright (c) 2012 Kevin Harwood
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "UIImageView+KHGravater.h"
#import "UIImageView+AFNetworking.h"
#import <CommonCrypto/CommonDigest.h>

NSString * const KHGravatarBaseURLString = @"https://secure.gravatar.com/avatar/";

static NSString * KHGravatarHashForEmailAddress(NSString *emailAddress) {
emailAddress = [emailAddress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
emailAddress = [emailAddress lowercaseString];

const char *cStr = [emailAddress UTF8String];
unsigned char result[16];
CC_MD5( cStr, strlen(cStr), result );
NSString* md5EmailAddress = [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
return [md5EmailAddress lowercaseString];
}

@interface UIImageView (_KHGravater)

- (NSURL*)gravatarURLForEmailAddress:(NSString*)emailAddress;
- (NSURL*)gravatarURLForEmailAddress:(NSString*)emailAddress defaultImageType:(KHGravatarDefaultImage)defaultImageType rating:(KHGravatarRating)rating;
- (NSURL*)gravatarURLForEmailAddress:(NSString*)emailAddress defaultImageURL:(NSURL*)defaultImageURL rating:(KHGravatarRating)rating;
- (CGFloat)imageSize;

-(NSString*)ratingStringForRating:(KHGravatarRating)rating;

@end

@implementation UIImageView (KHGravater)


- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress{
[self setImageWithURL:[self gravatarURLForEmailAddress:emailAddress]];
}


- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress placeholderImage:(UIImage*)placeholderImage{
[self setImageWithURL:[self gravatarURLForEmailAddress:emailAddress] placeholderImage:placeholderImage];
}

- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress placeholderImage:(UIImage*)placeholderImage defaultImageType:(KHGravatarDefaultImage)defaultImageType rating:(KHGravatarRating)rating{
[self setImageWithURL:[self gravatarURLForEmailAddress:emailAddress defaultImageType:defaultImageType rating:rating] placeholderImage:placeholderImage];
}

- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress
placeholderImage:(UIImage*)placeholderImage
defaultImageURL:(NSURL*)defaultImageURL
rating:(KHGravatarRating)rating{
[self setImageWithURL:[self gravatarURLForEmailAddress:emailAddress defaultImageURL:defaultImageURL rating:rating] placeholderImage:placeholderImage];
}


- (void)setImageWithGravaterEmailAddress:(NSString*)emailAddress
placeholderImage:(UIImage *)placeholderImage
defaultImageType:(KHGravatarDefaultImage)defaultImageType
rating:(KHGravatarRating)rating
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure{
[self setImageWithURLRequest:[NSURLRequest requestWithURL:[self gravatarURLForEmailAddress:emailAddress]]
placeholderImage:placeholderImage
success:success
failure:failure];
}

#pragma mark - Private
- (NSURL*)gravatarURLForEmailAddress:(NSString*)emailAddress{
return [self gravatarURLForEmailAddress:emailAddress defaultImageType:KHGravatarDefaultImageDefault rating:KHGravatarRatingG];
}

- (NSURL*)gravatarURLForEmailAddress:(NSString*)emailAddress defaultImageType:(KHGravatarDefaultImage)defaultImageType rating:(KHGravatarRating)rating{
NSString *emailHash = KHGravatarHashForEmailAddress(emailAddress);
NSString * urlString = [NSString stringWithFormat:@"%@%@.png?s=%0.0f",KHGravatarBaseURLString,emailHash,[self imageSize]];

if(defaultImageType!=KHGravatarDefaultImageDefault){
NSString * type = nil;
switch (defaultImageType) {
case KHGravatarDefaultImage404:
type = @"404";
break;
case KHGravatarDefaultImageMysteryMan:
type = @"mm";
break;
case KHGravatarDefaultImageIdenticon:
type = @"identicon";
break;
case KHGravatarDefaultImageMonsterId:
type = @"monsterid";
break;
case KHGravatarDefaultImageWavatar:
type = @"wavatar";
break;
case KHGravatarDefaultImageRetro:
type = @"retro";
break;
default:
break;
}
if(type){
urlString = [urlString stringByAppendingFormat:@"&d=%@",type];
}
}

NSString * ratingString = [self ratingStringForRating:rating];
if(ratingString){
urlString = [urlString stringByAppendingFormat:@"&r=%@",ratingString];
}

NSURL *url = [NSURL URLWithString:urlString];
return url;
}

- (NSURL*)gravatarURLForEmailAddress:(NSString*)emailAddress defaultImageURL:(NSURL*)defaultImageURL rating:(KHGravatarRating)rating{
NSString *emailHash = KHGravatarHashForEmailAddress(emailAddress);
NSString * urlString = [NSString stringWithFormat:@"%@%@.png?s=%0.0f",KHGravatarBaseURLString,emailHash,[self imageSize]];

urlString = [urlString stringByAppendingFormat:@"&d=%@",[defaultImageURL absoluteString]];

NSString * ratingString = [self ratingStringForRating:rating];
if(ratingString){
urlString = [urlString stringByAppendingFormat:@"&r=%@",ratingString];
}

NSURL *url = [NSURL URLWithString:urlString];
return url;
}

- (CGFloat)imageSize{
CGFloat imageSize = [[UIScreen mainScreen] scale] * MAX(CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds));
return MIN(imageSize,512);
}

-(NSString*)ratingStringForRating:(KHGravatarRating)rating{
NSString * ratingString = nil;
switch (rating) {
case KHGravatarRatingG:
ratingString = @"g";
break;
case KHGravatarRatingPG:
ratingString = @"pg";
break;
case KHGravatarRatingR:
ratingString = @"r";
break;
case KHGravatarRatingX:
ratingString = @"x";
default:
break;
}
return ratingString;
}

@end
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2012 Kevin Harwood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

0 comments on commit bdd60cc

Please sign in to comment.