SVGKFastImageView.m 10 KB

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