Просмотр исходного кода

Clean up FLEXImagePreviewViewController

Tanner Bennett лет назад: 6
Родитель
Сommit
b7baa219a4

+ 1 - 1
Classes/GlobalStateExplorers/FileBrowser/FLEXFileBrowserTableViewController.m

@@ -224,7 +224,7 @@
     if (isDirectory) {
         drillInViewController = [[[self class] alloc] initWithPath:fullPath];
     } else if (image) {
-        drillInViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
+        drillInViewController = [FLEXImagePreviewViewController forImage:image];
     } else {
         NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
         if (!fileData.length) {

+ 1 - 1
Classes/Network/FLEXNetworkTransactionDetailTableViewController.m

@@ -519,7 +519,7 @@ typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
         }
     } else if ([mimeType hasPrefix:@"image/"]) {
         UIImage *image = [UIImage imageWithData:data];
-        detailViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
+        detailViewController = [FLEXImagePreviewViewController forImage:image];
     } else if ([mimeType isEqual:@"application/x-plist"]) {
         id propertyList = [NSPropertyListSerialization propertyListWithData:data options:0 format:NULL error:NULL];
         detailViewController = [[FLEXWebViewController alloc] initWithText:[propertyList description]];

+ 1 - 1
Classes/ObjectExplorers/Sections/Shortcuts/FLEXImageShortcuts.m

@@ -35,7 +35,7 @@
 /// View image
 - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
     if (row == 0) {
-        return [[FLEXImagePreviewViewController alloc] initWithImage:self.image];
+        return [FLEXImagePreviewViewController forImage:self.image];
     }
 
     return [super viewControllerToPushForRow:row];

+ 11 - 37
Classes/ObjectExplorers/Sections/Shortcuts/FLEXLayerShortcuts.m

@@ -7,48 +7,22 @@
 //
 
 #import "FLEXLayerShortcuts.h"
+#import "FLEXShortcut.h"
 #import "FLEXImagePreviewViewController.h"
 
-@interface FLEXLayerShortcuts ()
-@property (nonatomic, readonly) CALayer *layer;
-@end
-
 @implementation FLEXLayerShortcuts
 
-#pragma mark - Internal
-
-- (CALayer *)layer {
-    return self.object;
-}
-
-#pragma mark - Internal
-
-- (UIViewController *)imagePreviewViewController {
-    if (!CGRectIsEmpty(self.layer.bounds)) {
-        UIGraphicsBeginImageContextWithOptions(self.layer.bounds.size, NO, 0.0);
-        CGContextRef imageContext = UIGraphicsGetCurrentContext();
-        [self.layer renderInContext:imageContext];
-        UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
-        UIGraphicsEndImageContext();
-        return [FLEXImagePreviewViewController forImage:previewImage];
-    }
-
-    return nil;
-}
-
-
-#pragma mark - Overrides
-
 + (instancetype)forObject:(CALayer *)layer {
-    return [self forObject:layer additionalRows:@[@"Preview"]];
-}
-
-- (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
-    if (row == 0) {
-        return [self imagePreviewViewController];
-    }
-
-    return [super viewControllerToPushForRow:row];
+    return [self forObject:layer additionalRows:@[
+        [FLEXActionShortcut title:@"Preview Image" subtitle:nil
+            viewer:^UIViewController *(id layer) {
+                return [FLEXImagePreviewViewController previewForLayer:layer];
+            }
+            accessoryType:^UITableViewCellAccessoryType(id layer) {
+                return UITableViewCellAccessoryDisclosureIndicator;
+            }
+        ]
+    ]];
 }
 
 @end

+ 2 - 13
Classes/ObjectExplorers/Sections/Shortcuts/FLEXViewShortcuts.m

@@ -46,18 +46,6 @@
     return [self viewControllerForView:view] ?: [self viewControllerForAncestralView:view];
 }
 
-+ (UIViewController *)imagePreviewViewControllerForView:(UIView *)view {
-    if (!CGRectIsEmpty(view.bounds)) {
-        CGSize viewSize = view.bounds.size;
-        UIGraphicsBeginImageContextWithOptions(viewSize, NO, 0.0);
-        [view drawViewHierarchyInRect:CGRectMake(0, 0, viewSize.width, viewSize.height) afterScreenUpdates:YES];
-        UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
-        UIGraphicsEndImageContext();
-        return [FLEXImagePreviewViewController forImage:previewImage];
-    }
-
-    return nil;
-}
 
 #pragma mark - Overrides
 
@@ -87,7 +75,7 @@
         ],
         [FLEXActionShortcut title:@"Preview Image" subtitle:nil
             viewer:^UIViewController *(id view) {
-                return [FLEXViewShortcuts imagePreviewViewControllerForView:view];
+                return [FLEXImagePreviewViewController previewForView:view];
             }
             accessoryType:^UITableViewCellAccessoryType(id view) {
                 return UITableViewCellAccessoryDisclosureIndicator;
@@ -96,6 +84,7 @@
     ]];
 }
 
+
 #pragma mark - Runtime Adjustment
 
 #define PropertyKey(suffix) kFLEXPropertyAttributeKey##suffix : @""

+ 2 - 0
Classes/Utility/FLEXUtility.h

@@ -73,6 +73,8 @@ NS_INLINE CGRect FLEXRectSetHeight(CGRect r, CGFloat height) {
 + (NSString *)stringForCGRect:(CGRect)rect;
 + (UIViewController *)viewControllerForView:(UIView *)view;
 + (UIViewController *)viewControllerForAncestralView:(UIView *)view;
++ (UIImage *)previewImageForView:(UIView *)view;
++ (UIImage *)previewImageForLayer:(CALayer *)layer;
 + (NSString *)detailDescriptionForView:(UIView *)view;
 + (UIImage *)circularImageWithColor:(UIColor *)color radius:(CGFloat)radius;
 + (UIColor *)hierarchyIndentPatternColor;

+ 28 - 0
Classes/Utility/FLEXUtility.m

@@ -66,6 +66,34 @@
     return nil;
 }
 
++ (UIImage *)previewImageForView:(UIView *)view
+{
+    if (CGRectIsEmpty(view.bounds)) {
+        return nil;
+    }
+    
+    CGSize viewSize = view.bounds.size;
+    UIGraphicsBeginImageContextWithOptions(viewSize, NO, 0.0);
+    [view drawViewHierarchyInRect:CGRectMake(0, 0, viewSize.width, viewSize.height) afterScreenUpdates:YES];
+    UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
+    UIGraphicsEndImageContext();
+    return previewImage;
+}
+
++ (UIImage *)previewImageForLayer:(CALayer *)layer
+{
+    if (CGRectIsEmpty(layer.bounds)) {
+        return nil;
+    }
+    
+    UIGraphicsBeginImageContextWithOptions(layer.bounds.size, NO, 0.0);
+    CGContextRef imageContext = UIGraphicsGetCurrentContext();
+    [layer renderInContext:imageContext];
+    UIImage *previewImage = UIGraphicsGetImageFromCurrentImageContext();
+    UIGraphicsEndImageContext();
+    return previewImage;
+}
+
 + (NSString *)detailDescriptionForView:(UIView *)view
 {
     return [NSString stringWithFormat:@"frame %@", [self stringForCGRect:view.frame]];

+ 2 - 1
Classes/ViewHierarchy/FLEXImagePreviewViewController.h

@@ -10,7 +10,8 @@
 
 @interface FLEXImagePreviewViewController : UIViewController
 
++ (instancetype)previewForView:(UIView *)view;
++ (instancetype)previewForLayer:(CALayer *)layer;
 + (instancetype)forImage:(UIImage *)image;
-- (id)initWithImage:(UIImage *)image;
 
 @end

+ 34 - 22
Classes/ViewHierarchy/FLEXImagePreviewViewController.m

@@ -11,24 +11,33 @@
 #import "FLEXUtility.h"
 
 @interface FLEXImagePreviewViewController () <UIScrollViewDelegate>
-
 @property (nonatomic) UIImage *image;
-
 @property (nonatomic) UIScrollView *scrollView;
 @property (nonatomic) UIImageView *imageView;
-
 @end
-
+#pragma mark -
 @implementation FLEXImagePreviewViewController
 
-+ (instancetype)forImage:(UIImage *)image
-{
+#pragma mark Initialization
+
++ (instancetype)previewForView:(UIView *)view {
+    return [self forImage:[FLEXUtility previewImageForView:view]];
+}
+
++ (instancetype)previewForLayer:(CALayer *)layer {
+    return [self forImage:[FLEXUtility previewImageForLayer:layer]];
+}
+
++ (instancetype)forImage:(UIImage *)image {
+    if (!image) {
+        return nil;
+    }
+    
     return [[self alloc] initWithImage:image];
 }
 
-- (id)initWithImage:(UIImage *)image
-{
-    self = [super initWithNibName:nil bundle:nil];
+- (id)initWithImage:(UIImage *)image {
+    self = [super init];
     if (self) {
         self.title = @"Preview";
         self.image = image;
@@ -36,11 +45,13 @@
     return self;
 }
 
-- (void)viewDidLoad
-{
+
+#pragma mark Lifecycle
+
+- (void)viewDidLoad {
     [super viewDidLoad];
     
-    self.view.backgroundColor = [FLEXColor scrollViewBackgroundColor];
+    self.view.backgroundColor = FLEXColor.scrollViewBackgroundColor;
     
     self.imageView = [[UIImageView alloc] initWithImage:self.image];
     self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
@@ -56,23 +67,25 @@
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionButtonPressed:)];
 }
 
-- (void)viewDidLayoutSubviews
-{
+- (void)viewDidLayoutSubviews {
     [self centerContentInScrollViewIfNeeded];
 }
 
-- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
-{
+
+#pragma mark UIScrollViewDelegate
+
+- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
     return self.imageView;
 }
 
-- (void)scrollViewDidZoom:(UIScrollView *)scrollView
-{
+- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
     [self centerContentInScrollViewIfNeeded];
 }
 
-- (void)centerContentInScrollViewIfNeeded
-{
+
+#pragma mark Private
+
+- (void)centerContentInScrollViewIfNeeded {
     CGFloat horizontalInset = 0.0;
     CGFloat verticalInset = 0.0;
     if (self.scrollView.contentSize.width < self.scrollView.bounds.size.width) {
@@ -84,8 +97,7 @@
     self.scrollView.contentInset = UIEdgeInsetsMake(verticalInset, horizontalInset, verticalInset, horizontalInset);
 }
 
-- (void)actionButtonPressed:(id)sender
-{
+- (void)actionButtonPressed:(id)sender {
     static BOOL canSaveToCameraRoll = NO, didShowWarning = NO;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{