Skip to content

Instantly share code, notes, and snippets.

@sshrpe
Created May 10, 2011 11:24
Show Gist options
  • Save sshrpe/964297 to your computer and use it in GitHub Desktop.
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:
//SMSBackgroundImageView.h
#import <UIKit/UIKit.h>
@interface SMSBackgroundImageView : UIView {
}
@property (nonatomic, retain) UIImage *backgroundImage;
@end
//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