Skip to content

Instantly share code, notes, and snippets.

@AlexRezit
Created January 30, 2013 01:34
Show Gist options
  • Save AlexRezit/4669828 to your computer and use it in GitHub Desktop.
Save AlexRezit/4669828 to your computer and use it in GitHub Desktop.
//
// 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
//
// 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