Created
January 30, 2013 01:34
-
-
Save AlexRezit/4669828 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImageView+RatingStar.h | |
// RatingImageDemo | |
// | |
// Created by Alex Rezit on 25/01/2013. | |
// Copyright (c) 2013 Seymour Dev. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImageView (RatingStar) | |
- (void)setImageWithRating:(float)rating; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImageView+RatingStar.m | |
// RatingImageDemo | |
// | |
// Created by Alex Rezit on 25/01/2013. | |
// Copyright (c) 2013 Seymour Dev. All rights reserved. | |
// | |
#import "UIImageView+RatingStar.h" | |
float const kRatingFullScore = 10.0f; | |
NSUInteger const kRatingNumberOfStars = 5; | |
@implementation UIImageView (RatingStar) | |
- (void)setImageWithRating:(float)rating | |
{ | |
UIImage *filledStar = [UIImage imageNamed:@"rating_star_filled"]; | |
UIImage *hollowStar = [UIImage imageNamed:@"rating_star_hollow"]; | |
CGFloat width = filledStar.size.width; | |
CGFloat height = filledStar.size.height; | |
CGSize imageSize = CGSizeMake(width * kRatingNumberOfStars, height); | |
UIGraphicsBeginImageContext(imageSize); | |
[hollowStar drawAsPatternInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)]; | |
[filledStar drawAsPatternInRect:CGRectMake(0, 0, width * (rating / kRatingFullScore * kRatingNumberOfStars), height)]; | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
self.image = image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment