forked from SVGKit/SVGKit
/
SVGImageElement.m
121 lines (85 loc) · 3.61 KB
/
SVGImageElement.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#import "SVGImageElement.h"
#import "SVGHelperUtilities.h"
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#endif
#if TARGET_OS_IPHONE
#define SVGImage UIImage
#else
#define SVGImage CIImage
#endif
#define SVGImageRef SVGImage*
CGImageRef SVGImageCGImage(SVGImageRef img)
{
#if TARGET_OS_IPHONE
return img.CGImage;
#else
NSBitmapImageRep* rep = [[[NSBitmapImageRep alloc] initWithCIImage:img] autorelease];
return rep.CGImage;
#endif
}
@interface SVGImageElement()
@property (nonatomic, retain, readwrite) NSString *href;
@end
@implementation SVGImageElement
@synthesize transform; // each SVGElement subclass that conforms to protocol "SVGTransformable" has to re-synthesize this to work around bugs in Apple's Objective-C 2.0 design that don't allow @properties to be extended by categories / protocols
@synthesize viewBox; // each SVGElement subclass that conforms to protocol "SVGFitToViewBox" has to re-synthesize this to work around bugs in Apple's Objective-C 2.0 design that don't allow @properties to be extended by categories / protocols
@synthesize x = _x;
@synthesize y = _y;
@synthesize width = _width;
@synthesize height = _height;
@synthesize href = _href;
- (void)dealloc {
[_href release], _href = nil;
[super dealloc];
}
- (void)postProcessAttributesAddingErrorsTo:(SVGKParseResult *)parseResult {
[super postProcessAttributesAddingErrorsTo:parseResult];
if( [[self getAttribute:@"x"] length] > 0 )
_x = [[self getAttribute:@"x"] floatValue];
if( [[self getAttribute:@"y"] length] > 0 )
_y = [[self getAttribute:@"y"] floatValue];
if( [[self getAttribute:@"width"] length] > 0 )
_width = [[self getAttribute:@"width"] floatValue];
if( [[self getAttribute:@"height"] length] > 0 )
_height = [[self getAttribute:@"height"] floatValue];
if( [[self getAttribute:@"href"] length] > 0 )
self.href = [self getAttribute:@"href"];
}
- (CALayer *) newLayer
{
CAShapeLayer* newLayer = [[CALayer alloc] init];
newLayer.name = self.identifier;
[newLayer setValue:self.identifier forKey:kSVGElementIdentifier];
/** transform our LOCAL path into ABSOLUTE space */
CGRect frame = CGRectMake(_x, _y, _width, _height);
frame = CGRectApplyAffineTransform(frame, [SVGHelperUtilities transformAbsoluteIncludingViewportForTransformableOrViewportEstablishingElement:self]);
newLayer.frame = frame;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_href]];
SVGImageRef image = [SVGImage imageWithData:imageData];
newLayer.contents = (id)SVGImageCGImage(image);
#if OLD_CODE
__block CALayer *layer = [[CALayer layer] retain];
layer.name = self.identifier;
[layer setValue:self.identifier forKey:kSVGElementIdentifier];
CGRect frame = CGRectMake(_x, _y, _width, _height);
frame = CGRectApplyAffineTransform(frame, [SVGHelperUtilities transformAbsoluteIncludingViewportForTransformableOrViewportEstablishingElement:self]);
layer.frame = frame;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_href]];
SVGImageRef image = [SVGImage imageWithData:imageData];
// _href = @"http://b.dryicons.com/images/icon_sets/coquette_part_4_icons_set/png/128x128/png_file.png";
// NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_href]];
// UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
layer.contents = (id)SVGImageCGImage(image);
});
});
return layer;
#endif
return newLayer;
}
- (void)layoutLayer:(CALayer *)layer {
}
@end