SVGKLayeredImageView.m 3.5 KB

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