Created
May 10, 2011 11:24
-
-
Save sshrpe/964297 to your computer and use it in GitHub Desktop.
Background Image View, for creating views with textured backgrounds without using UIColor's memory-hogging colorWithPatternImage:
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
//SMSBackgroundImageView.h | |
#import <UIKit/UIKit.h> | |
@interface SMSBackgroundImageView : UIView { | |
} | |
@property (nonatomic, retain) UIImage *backgroundImage; | |
@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
//SMSBackgroundImageView.m | |
#import "SMSBackgroundImageView.h" | |
@implementation SMSBackgroundImageView | |
@synthesize backgroundImage; | |
-(void)setBackgroundImage:(UIImage *)image; | |
{ | |
if (backgroundImage != nil) { | |
[backgroundImage autorelease]; | |
} | |
backgroundImage = [image retain]; | |
[self setNeedsDisplay]; | |
} | |
- (void)drawRect:(CGRect)rect | |
{ | |
[super drawRect:rect]; | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
if (self.backgroundImage != nil) { | |
CGImageRef image = [self.backgroundImage CGImage]; | |
CGSize imageSize = self.backgroundImage.size; | |
CGContextDrawTiledImage(context, CGRectMake(0, 0, imageSize.width, imageSize.height), image); | |
} | |
} | |
- (void)dealloc | |
{ | |
self.backgroundImage = nil; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment