SVGKFastImageView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #import "SVGKFastImageView.h"
  2. @interface SVGKFastImageView ()
  3. @property(nonatomic,readwrite) NSTimeInterval timeIntervalForLastReRenderOfSVGFromMemory;
  4. @property (nonatomic, strong) NSDate* startRenderTime, * endRenderTime; /**< for debugging, lets you know how long it took to add/generate the CALayer (may have been cached! Only SVGKImage knows true times) */
  5. @property (nonatomic) BOOL didRegisterObservers, didRegisterInternalRedrawObservers;
  6. @end
  7. @implementation SVGKFastImageView
  8. {
  9. NSString* internalContextPointerBecauseApplesDemandsIt;
  10. }
  11. @synthesize image = _image;
  12. @synthesize tileRatio = _tileRatio;
  13. @synthesize disableAutoRedrawAtHighestResolution = _disableAutoRedrawAtHighestResolution;
  14. @synthesize timeIntervalForLastReRenderOfSVGFromMemory = _timeIntervalForLastReRenderOfSVGFromMemory;
  15. - (id)init
  16. {
  17. NSAssert(false, @"init not supported, use initWithSVGKImage:");
  18. return nil;
  19. }
  20. - (id)initWithCoder:(NSCoder *)aDecoder
  21. {
  22. self = [super initWithCoder:aDecoder];
  23. if( self )
  24. {
  25. [self populateFromImage:nil];
  26. }
  27. return self;
  28. }
  29. -(id)initWithFrame:(CGRect)frame
  30. {
  31. self = [super initWithFrame:frame];
  32. if( self )
  33. {
  34. #if SVGKIT_UIKIT
  35. self.backgroundColor = [UIColor clearColor];
  36. #else
  37. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  38. #endif
  39. }
  40. return self;
  41. }
  42. - (id)initWithSVGKImage:(SVGKImage*) im
  43. {
  44. self = [super init];
  45. if (self)
  46. {
  47. [self populateFromImage:im];
  48. }
  49. return self;
  50. }
  51. - (void)populateFromImage:(SVGKImage*) im
  52. {
  53. #if SVGKIT_MAC && USE_SUBLAYERS_INSTEAD_OF_BLIT
  54. // setup layer-backed view
  55. self.wantsLayer = YES;
  56. #endif
  57. if( im == nil )
  58. {
  59. SVGKitLogWarn(@"[%@] WARNING: you have initialized an SVGKImageView 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]);
  60. }
  61. self.image = im;
  62. self.frame = CGRectMake( 0,0, im.size.width, im.size.height ); // NB: this uses the default SVG Viewport; an ImageView can theoretically calc a new viewport (but its hard to get right!)
  63. self.tileRatio = CGSizeZero;
  64. #if SVGKIT_UIKIT
  65. self.backgroundColor = [UIColor clearColor];
  66. #else
  67. self.layer.backgroundColor = [NSColor clearColor].CGColor;
  68. #endif
  69. }
  70. - (void)setImage:(SVGKImage *)image {
  71. if( !internalContextPointerBecauseApplesDemandsIt ) {
  72. internalContextPointerBecauseApplesDemandsIt = @"Apple wrote the addObserver / KVO notification API wrong in the first place and now requires developers to pass around pointers to fake objects to make up for the API deficicineces. You have to have one of these pointers per object, and they have to be internal and private. They serve no real value.";
  73. }
  74. [_image removeObserver:self forKeyPath:@"size" context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  75. _image = image;
  76. /** redraw-observers */
  77. if( self.disableAutoRedrawAtHighestResolution )
  78. ;
  79. else {
  80. [self addInternalRedrawOnResizeObservers];
  81. [_image addObserver:self forKeyPath:@"size" options:NSKeyValueObservingOptionNew context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  82. }
  83. /** other obeservers */
  84. if (!self.didRegisterObservers) {
  85. self.didRegisterObservers = true;
  86. [self addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  87. [self addObserver:self forKeyPath:@"tileRatio" options:NSKeyValueObservingOptionNew context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  88. [self addObserver:self forKeyPath:@"showBorder" options:NSKeyValueObservingOptionNew context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  89. }
  90. }
  91. -(void) addInternalRedrawOnResizeObservers
  92. {
  93. if (self.didRegisterInternalRedrawObservers) return;
  94. self.didRegisterInternalRedrawObservers = true;
  95. [self addObserver:self forKeyPath:@"layer" options:NSKeyValueObservingOptionNew context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  96. [self.layer addObserver:self forKeyPath:@"transform" options:NSKeyValueObservingOptionNew context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  97. }
  98. -(void) removeInternalRedrawOnResizeObservers
  99. {
  100. if (!self.didRegisterInternalRedrawObservers) return;
  101. [self removeObserver:self forKeyPath:@"layer" context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  102. [self.layer removeObserver:self forKeyPath:@"transform" context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  103. self.didRegisterInternalRedrawObservers = false;
  104. }
  105. -(void)setDisableAutoRedrawAtHighestResolution:(BOOL)newValue
  106. {
  107. if( newValue == _disableAutoRedrawAtHighestResolution )
  108. return;
  109. _disableAutoRedrawAtHighestResolution = newValue;
  110. if( self.disableAutoRedrawAtHighestResolution ) // disabled, so we have to remove the observers
  111. {
  112. [self removeInternalRedrawOnResizeObservers];
  113. }
  114. else // newly-enabled ... must add the observers
  115. {
  116. [self addInternalRedrawOnResizeObservers];
  117. }
  118. }
  119. - (void)dealloc
  120. {
  121. if( self.disableAutoRedrawAtHighestResolution )
  122. ;
  123. else
  124. [self removeInternalRedrawOnResizeObservers];
  125. if (self.didRegisterObservers) {
  126. [self removeObserver:self forKeyPath:@"image" context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  127. [self removeObserver:self forKeyPath:@"tileRatio" context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  128. [self removeObserver:self forKeyPath:@"showBorder" context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  129. }
  130. [_image removeObserver:self forKeyPath:@"size" context:(__bridge void * _Nullable)(internalContextPointerBecauseApplesDemandsIt)];
  131. _image = nil;
  132. }
  133. /** Trigger a call to re-display (at higher or lower draw-resolution) (get Apple to call drawRect: again) */
  134. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  135. {
  136. if( [keyPath isEqualToString:@"transform"] && CGSizeEqualToSize( CGSizeZero, self.tileRatio ) )
  137. {
  138. /*SVGKitLogVerbose(@"transform changed. Setting layer scale: %2.2f --> %2.2f", self.layer.contentsScale, self.transform.a);
  139. self.layer.contentsScale = self.transform.a;*/
  140. [self.image.CALayerTree removeFromSuperlayer]; // force apple to redraw?
  141. #if SVGKIT_UIKIT
  142. [self setNeedsDisplay];
  143. #else
  144. [self setNeedsDisplay:YES];
  145. #endif
  146. }
  147. else
  148. {
  149. if( self.disableAutoRedrawAtHighestResolution )
  150. ;
  151. else
  152. {
  153. #if SVGKIT_UIKIT
  154. [self setNeedsDisplay];
  155. #else
  156. [self setNeedsDisplay:YES];
  157. #endif
  158. }
  159. }
  160. }
  161. /**
  162. NB: this implementation is a bit tricky, because we're extending Apple's concept of a UIView to add "tiling"
  163. and "automatic rescaling"
  164. */
  165. -(void)drawRect:(CGRect)rect
  166. {
  167. self.startRenderTime = self.endRenderTime = [NSDate date];
  168. /**
  169. view.bounds == width and height of the view
  170. imageBounds == natural width and height of the SVGKImage
  171. */
  172. CGRect imageBounds = CGRectMake( 0,0, self.image.size.width, self.image.size.height );
  173. /** Check if tiling is enabled in either direction
  174. We have to do this FIRST, because we cannot extend Apple's enum they use for UIViewContentMode
  175. (objective-C is a weak language).
  176. If we find ANY tiling, we will be forced to skip the UIViewContentMode handling
  177. TODO: it would be nice to combine the two - e.g. if contentMode=BottomRight, then do the tiling with
  178. the bottom right corners aligned. If = TopLeft, then tile with the top left corners aligned,
  179. etc.
  180. */
  181. int cols = ceil(self.tileRatio.width);
  182. int rows = ceil(self.tileRatio.height);
  183. if( cols < 1 ) // It's meaningless to have "fewer than 1" tiles; this lets us ALSO handle special case of "CGSizeZero == disable tiling"
  184. cols = 1;
  185. if( rows < 1 ) // It's meaningless to have "fewer than 1" tiles; this lets us ALSO handle special case of "CGSizeZero == disable tiling"
  186. rows = 1;
  187. CGSize scaleConvertImageToView;
  188. CGSize tileSize;
  189. if( cols == 1 && rows == 1 ) // if we are NOT tiling, then obey the UIViewContentMode as best we can!
  190. {
  191. #ifdef USE_SUBLAYERS_INSTEAD_OF_BLIT
  192. if( self.image.CALayerTree.superlayer == self.layer )
  193. {
  194. [super drawRect:rect];
  195. return; // TODO: Apple's bugs - they ignore all attempts to force a redraw
  196. }
  197. else
  198. {
  199. [self.layer addSublayer:self.image.CALayerTree];
  200. return; // we've added the layer - let Apple take care of the rest!
  201. }
  202. #else
  203. scaleConvertImageToView = CGSizeMake( self.bounds.size.width / imageBounds.size.width, self.bounds.size.height / imageBounds.size.height );
  204. tileSize = self.bounds.size;
  205. #endif
  206. }
  207. else
  208. {
  209. scaleConvertImageToView = CGSizeMake( self.bounds.size.width / (self.tileRatio.width * imageBounds.size.width), self.bounds.size.height / ( self.tileRatio.height * imageBounds.size.height) );
  210. tileSize = CGSizeMake( self.bounds.size.width / self.tileRatio.width, self.bounds.size.height / self.tileRatio.height );
  211. }
  212. //DEBUG: SVGKitLogVerbose(@"cols, rows: %i, %i ... scaleConvert: %@ ... tilesize: %@", cols, rows, NSStringFromCGSize(scaleConvertImageToView), NSStringFromCGSize(tileSize) );
  213. /** To support tiling, and to allow internal shrinking, we use renderInContext */
  214. #if SVGKIT_UIKIT
  215. CGContextRef context = UIGraphicsGetCurrentContext();
  216. #else
  217. CGContextRef context = SVGKGraphicsGetCurrentContext();
  218. #endif
  219. for( int k=0; k<rows; k++ )
  220. for( int i=0; i<cols; i++ )
  221. {
  222. CGContextSaveGState(context);
  223. CGContextTranslateCTM(context, i * tileSize.width, k * tileSize.height );
  224. CGContextScaleCTM( context, scaleConvertImageToView.width, scaleConvertImageToView.height );
  225. [self.image renderInContext:context];
  226. CGContextRestoreGState(context);
  227. }
  228. /** The border is VERY helpful when debugging rendering and touch / hit detection problems! */
  229. if( self.showBorder )
  230. {
  231. [[UIColor blackColor] set];
  232. CGContextStrokeRect(context, rect);
  233. }
  234. self.endRenderTime = [NSDate date];
  235. self.timeIntervalForLastReRenderOfSVGFromMemory = [self.endRenderTime timeIntervalSinceDate:self.startRenderTime];
  236. }
  237. #if SVGKIT_MAC
  238. static CGContextRef SVGKGraphicsGetCurrentContext(void) {
  239. NSGraphicsContext *context = NSGraphicsContext.currentContext;
  240. #pragma clang diagnostic push
  241. #pragma clang diagnostic ignored "-Wunguarded-availability"
  242. if ([context respondsToSelector:@selector(CGContext)]) {
  243. return context.CGContext;
  244. } else {
  245. return context.graphicsPort;
  246. }
  247. #pragma clang diagnostic pop
  248. }
  249. #endif
  250. @end