FLEXLayerExplorerViewController.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // FLEXLayerExplorerViewController.m
  3. // UICatalog
  4. //
  5. // Created by Ryan Olson on 12/14/14.
  6. // Copyright (c) 2014 f. All rights reserved.
  7. //
  8. #import "FLEXLayerExplorerViewController.h"
  9. #import "FLEXImagePreviewViewController.h"
  10. typedef NS_ENUM(NSUInteger, FLEXLayerExplorerRow) {
  11. FLEXLayerExplorerRowPreview
  12. };
  13. @interface FLEXLayerExplorerViewController ()
  14. @property (nonatomic, readonly) CALayer *layerToExplore;
  15. @end
  16. @implementation FLEXLayerExplorerViewController
  17. - (CALayer *)layerToExplore
  18. {
  19. return [self.object isKindOfClass:[CALayer class]] ? self.object : nil;
  20. }
  21. #pragma mark - Superclass Overrides
  22. - (NSString *)customSectionTitle
  23. {
  24. return @"Shortcuts";
  25. }
  26. - (NSArray *)customSectionRowCookies
  27. {
  28. return @[@(FLEXLayerExplorerRowPreview)];
  29. }
  30. - (NSString *)customSectionTitleForRowCookie:(id)rowCookie
  31. {
  32. NSString *title = nil;
  33. if ([rowCookie isKindOfClass:[NSNumber class]]) {
  34. FLEXLayerExplorerRow row = [rowCookie unsignedIntegerValue];
  35. switch (row) {
  36. case FLEXLayerExplorerRowPreview:
  37. title = @"Preview Image";
  38. break;
  39. }
  40. }
  41. return title;
  42. }
  43. - (BOOL)customSectionCanDrillIntoRowWithCookie:(id)rowCookie
  44. {
  45. return YES;
  46. }
  47. - (UIViewController *)customSectionDrillInViewControllerForRowCookie:(id)rowCookie
  48. {
  49. UIViewController *drillInViewController = nil;
  50. if ([rowCookie isKindOfClass:[NSNumber class]]) {
  51. FLEXLayerExplorerRow row = [rowCookie unsignedIntegerValue];
  52. switch (row) {
  53. case FLEXLayerExplorerRowPreview:
  54. drillInViewController = [[self class] imagePreviewViewControllerForLayer:self.layerToExplore];
  55. break;
  56. }
  57. }
  58. return drillInViewController;
  59. }
  60. + (UIViewController *)imagePreviewViewControllerForLayer:(CALayer *)layer
  61. {
  62. UIViewController *imagePreviewViewController = nil;
  63. if (!CGRectIsEmpty(layer.bounds)) {
  64. UIGraphicsBeginImageContextWithOptions(layer.bounds.size, NO, 0.0);
  65. CGContextRef imageContext = UIGraphicsGetCurrentContext();
  66. [layer renderInContext:imageContext];
  67. UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
  68. UIGraphicsEndImageContext();
  69. imagePreviewViewController = [[FLEXImagePreviewViewController alloc] initWithImage:previewImage];
  70. }
  71. return imagePreviewViewController;
  72. }
  73. @end