This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
IAPatternBackgroundRenderer.m
84 lines (70 loc) · 2.71 KB
/
IAPatternBackgroundRenderer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// IAPatternBackgroundRenderer.m
// ImageAlpha
//
// Created by (ja) xx on 12.mar.12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "IAPatternBackgroundRenderer.h"
#import <QuartzCore/QuartzCore.h>
static void drawPatternImage(void *info, CGContextRef ctx)
{
CGImageRef image = (CGImageRef)info;
CGContextDrawImage(ctx, CGRectMake(0,0, CGImageGetWidth(image),CGImageGetHeight(image)), image);
}
@implementation IAPatternBackgroundRenderer
- (id)init
{
self = [super init];
if (self) {
bgLayer = [CALayer new];
NSMutableDictionary *newActions = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[NSNull null], @"backgroundColor",
[NSNull null], @"contents",
[NSNull null], @"bounds",
nil];
bgLayer.actions = newActions; // non-animated scroll
[newActions release];
}
return self;
}
-(BOOL)canMove {
return YES;
}
-(CALayer *)getLayer {
CALayer *newLayer = [CALayer new];
newLayer.actions = [[bgLayer.actions copy] autorelease];
CGColorRef color = CGColorCreateCopy(bgLayer.backgroundColor);
newLayer.backgroundColor = color;
CGColorRelease(color);
return [newLayer autorelease];
}
-(void)dealloc {
if (image) CGImageRelease(image);
[bgLayer release];
[super dealloc];
}
-(void)setTileImage:(NSImage*)nsimage {
if (image) CGImageRelease(image);
[NSGraphicsContext saveGraphicsState];
image = [nsimage CGImageForProposedRect:&(NSRect){.size=[nsimage size]} context:NULL hints:[NSDictionary dictionary]];
CGImageRetain(image);
[NSGraphicsContext restoreGraphicsState];
}
-(void)tileLayerAtX:(NSNumber*)x Y:(NSNumber *)y {
CGFloat width = CGImageGetWidth(image), height = CGImageGetHeight(image);
CGPatternRef pattern = CGPatternCreate( image,
CGRectMake(0, 0, width, height),
CGAffineTransformMake (1, 0, 0, 1, [x doubleValue], [y doubleValue]),
width, height,
kCGPatternTilingConstantSpacing,
true,
&(CGPatternCallbacks){0, &drawPatternImage, 0});
CGColorSpaceRef space = CGColorSpaceCreatePattern(NULL);
CGColorRef color = CGColorCreateWithPattern(space, pattern, &(CGFloat){1.0});
CGColorSpaceRelease(space);
CGPatternRelease(pattern);
bgLayer.backgroundColor = color; //set your layer's background to the image
CGColorRelease(color);
}
@end