ARTPattern.m 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <React/ARTPattern.h>
  8. #import <React/RCTLog.h>
  9. #import "RCTConvert+ART.h"
  10. @implementation ARTPattern
  11. {
  12. CGImageRef _image;
  13. CGRect _rect;
  14. }
  15. - (instancetype)initWithArray:(NSArray<id /* imagesource + numbers */> *)array
  16. {
  17. if ((self = [super initWithArray:array])) {
  18. if (array.count < 6) {
  19. RCTLogError(@"-[%@ %@] expects 6 elements, received %@",
  20. self.class, NSStringFromSelector(_cmd), array);
  21. return nil;
  22. }
  23. _image = CGImageRetain([RCTConvert CGImage:array[1]]);
  24. _rect = [RCTConvert CGRect:array offset:2];
  25. }
  26. return self;
  27. }
  28. - (void)dealloc
  29. {
  30. CGImageRelease(_image);
  31. }
  32. // Note: This could use applyFillColor with a pattern. This could be more efficient but
  33. // to do that, we need to calculate our own user space CTM.
  34. - (void)paint:(CGContextRef)context
  35. {
  36. CGContextDrawTiledImage(context, _rect, _image);
  37. }
  38. @end