SVGKFastImageView.m 10 KB

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