SVGKLayeredImageView.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #import "SVGKLayeredImageView.h"
  2. #import <QuartzCore/QuartzCore.h>
  3. #import "SVGKSourceString.h"
  4. #import "SVGKInlineResource.h"
  5. @interface SVGKLayeredImageView()
  6. @property(nonatomic,strong) CAShapeLayer* internalBorderLayer;
  7. @end
  8. @implementation SVGKLayeredImageView
  9. @synthesize internalBorderLayer = _internalBorderLayer;
  10. /** uses the custom SVGKLayer instead of a default CALayer */
  11. +(Class)layerClass
  12. {
  13. return NSClassFromString(@"SVGKLayer");
  14. }
  15. - (id)init
  16. {
  17. NSAssert(false, @"init not supported, use initWithSVGKImage:");
  18. return nil;
  19. }
  20. - (id)initWithCoder:(NSCoder *)aDecoder
  21. {
  22. if( aDecoder == nil )
  23. {
  24. self = [super initWithFrame:CGRectMake(0,0,100,100)]; // coincides with the inline SVG in populateFromImage!
  25. }
  26. else
  27. {
  28. self = [super initWithCoder:aDecoder];
  29. }
  30. if( self )
  31. {
  32. [self populateFromImage:nil];
  33. }
  34. return self;
  35. }
  36. -(id)initWithFrame:(CGRect)frame
  37. {
  38. self = [super initWithFrame:frame];
  39. if( self )
  40. {
  41. [self populateFromImage:nil];
  42. }
  43. return self;
  44. }
  45. - (id)initWithSVGKImage:(SVGKImage*) im
  46. {
  47. if( im == nil )
  48. {
  49. self = [super initWithFrame:CGRectMake(0,0,100,100)]; // coincides with the inline SVG in populateFromImage!
  50. }
  51. else
  52. {
  53. self = [super initWithFrame:CGRectMake( 0,0, im.CALayerTree.frame.size.width, im.CALayerTree.frame.size.height )]; // default: 0,0 to width x height of original image];
  54. }
  55. if (self)
  56. {
  57. [self populateFromImage:im];
  58. }
  59. return self;
  60. }
  61. - (void)populateFromImage:(SVGKImage*) im
  62. {
  63. #if SVGKIT_MAC
  64. // setup layer-hosting view
  65. self.layer = [[SVGKLayer alloc] init];
  66. self.wantsLayer = YES;
  67. #endif
  68. if( im == nil )
  69. {
  70. #ifndef SVGK_DONT_USE_EMPTY_IMAGE_PLACEHOLDER
  71. SVGKitLogWarn(@"[%@] WARNING: you have initialized an [%@] with a blank image (nil). Possibly because you're using Storyboards or NIBs which Apple won't allow us to decorate. Make sure you assign an SVGKImage to the .image property!", [self class], [self class]);
  72. #if SVGKIT_UIKIT
  73. self.backgroundColor = [UIColor clearColor];
  74. #else
  75. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  76. #endif
  77. NSString* svgStringDefaultContents = SVGKGetDefaultContentString();
  78. SVGKitLogInfo(@"About to make a blank image using the inlined SVG = %@", svgStringDefaultContents);
  79. SVGKImage* defaultBlankImage = [SVGKImage imageWithSource:[SVGKSourceString sourceFromContentsOfString:svgStringDefaultContents]];
  80. ((SVGKLayer*) self.layer).SVGImage = defaultBlankImage;
  81. #endif
  82. }
  83. else
  84. {
  85. #if SVGKIT_UIKIT
  86. self.backgroundColor = [UIColor clearColor];
  87. #else
  88. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  89. #endif
  90. ((SVGKLayer*) self.layer).SVGImage = im;
  91. }
  92. }
  93. /** Delegate the call to the internal layer that's coded to handle this stuff automatically */
  94. -(SVGKImage *)image
  95. {
  96. return ((SVGKLayer*)self.layer).SVGImage;
  97. }
  98. /** Delegate the call to the internal layer that's coded to handle this stuff automatically */
  99. -(void)setImage:(SVGKImage *)image
  100. {
  101. ((SVGKLayer*)self.layer).SVGImage = image;
  102. }
  103. /** Delegate the call to the internal layer that's coded to handle this stuff automatically */
  104. -(BOOL)showBorder
  105. {
  106. return ((SVGKLayer*)self.layer).showBorder;
  107. }
  108. /** Delegate the call to the internal layer that's coded to handle this stuff automatically */
  109. -(void)setShowBorder:(BOOL)showBorder
  110. {
  111. ((SVGKLayer*)self.layer).showBorder = showBorder;
  112. }
  113. -(NSTimeInterval)timeIntervalForLastReRenderOfSVGFromMemory
  114. {
  115. return[((SVGKLayer*)self.layer).endRenderTime timeIntervalSinceDate:((SVGKLayer*)self.layer).startRenderTime];
  116. }
  117. @end