/
UIView+DebugQuickLook.m
53 lines (45 loc) · 1.52 KB
/
UIView+DebugQuickLook.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// UIView+DebugQuickLook.m
//
// Created by Tom Adriaenssen on 14/03/14.
// Copyright (c) 2014 Tom Adriaenssen. All rights reserved.
//
#import <objc/runtime.h>
@implementation UIView (DebugQuickLook)
#ifdef DEBUG
+ (void)insert_debugQuickLookObject
{
SEL debugQuickLookObject = NSSelectorFromString(@"debugQuickLookObject");
// dont add it if it's already there
if ([self instancesRespondToSelector:debugQuickLookObject]) {
return;
}
SEL image_debugQuickLookObject = @selector(image_debugQuickLookObject);
IMP imp = class_getMethodImplementation(self, image_debugQuickLookObject);
const char* encoding = method_getTypeEncoding(class_getClassMethod(self, image_debugQuickLookObject));
BOOL ok = class_addMethod(self, debugQuickLookObject, imp, encoding);
if (ok) {
NSLog(@"added debugQuickLookObject support to UIView.");
}
else {
NSLog(@"Could not add debugQuickLookObject support to UIView. Bummer.");
}
}
- (id)image_debugQuickLookObject
{
if (self.bounds.size.width == 0 || self.bounds.size.height == 0)
return nil;
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
if (![self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES])
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (void)load {
@autoreleasepool {
[self insert_debugQuickLookObject];
}
}
#endif
@end