Created
November 23, 2018 03:41
-
-
Save yycking/41cc571d4878b51ccd37774bfd6ecb04 to your computer and use it in GitHub Desktop.
fix: UIImagePickerController cannot move editing box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| NS_ASSUME_NONNULL_BEGIN | |
| @interface UIImagePickerController (FixCannotMoveEditingBox) | |
| - (void)fixCannotMoveEditingBox; | |
| @end | |
| NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "UIImagePickerController+fixCannotMoveEditingBox.h" | |
| #import <objc/runtime.h> | |
| @interface WeakObjectContainer : NSObject | |
| @property (nonatomic, readonly, weak) id object; | |
| @end | |
| @implementation WeakObjectContainer | |
| - (instancetype) initWithObject:(id)object | |
| { | |
| if (!(self = [super init])) | |
| return nil; | |
| _object = object; | |
| return self; | |
| } | |
| @end | |
| @implementation UIImagePickerController (FixCannotMoveEditingBox) | |
| // MARK: - Create a weak stored property in extension | |
| - (id)weakObjectForKey:(const void*)key { | |
| WeakObjectContainer *container = objc_getAssociatedObject(self, key); | |
| return [container object]; | |
| } | |
| - (void)setWeakObject:(id)object forKey:(const void*)key { | |
| WeakObjectContainer *container = [[WeakObjectContainer alloc] initWithObject:object]; | |
| objc_setAssociatedObject(self, key, container, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
| } | |
| // MARK: - Create a weak property for scrollView | |
| - (UIScrollView *)scrollView { | |
| return [self weakObjectForKey:_cmd]; | |
| } | |
| - (void)setScrollView:(UIScrollView *)scrollView { | |
| [self setWeakObject:scrollView forKey:@selector(scrollView)]; | |
| } | |
| // MARK: - Create a weak property for cropView | |
| - (UIView *)cropView { | |
| return [self weakObjectForKey:_cmd]; | |
| } | |
| - (void)setCropView:(UIView *)cropView { | |
| [self setWeakObject:cropView forKey:@selector(cropView)]; | |
| } | |
| // MARK: - Fix cannot move editing box | |
| - (void)fixCannotMoveEditingBox { | |
| if (self.sourceType == UIImagePickerControllerSourceTypeCamera && | |
| (!self.scrollView || !self.cropView)) { | |
| UIView *view = [self view]; | |
| self.scrollView = [self findScrollView:view]; | |
| self.cropView = [self findCropView:view]; | |
| if (self.scrollView && self.cropView) { | |
| CGFloat top = self.cropView.frame.origin.y; | |
| CGFloat bottom = ({ | |
| self.scrollView.frame.size.height - self.cropView.frame.size.height - self.cropView.frame.origin.y; | |
| }); | |
| self.scrollView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0); | |
| self.scrollView.contentOffset = CGPointMake(0, -top+10); | |
| } | |
| } | |
| __weak typeof(self) weakself = self; | |
| dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC); | |
| dispatch_after(delay, dispatch_get_main_queue(), ^(void){ | |
| __strong typeof(weakself) strongself = weakself; | |
| [strongself fixCannotMoveEditingBox]; | |
| }); | |
| } | |
| - (UIScrollView *)findScrollView:(UIView *)view { | |
| if ([view isKindOfClass:[UIScrollView class]]) { | |
| return (UIScrollView *)view; | |
| } | |
| for (UIView *subview in [view subviews]) { | |
| UIScrollView *view = [self findScrollView:subview]; | |
| if (view) { | |
| return view; | |
| } | |
| } | |
| return nil; | |
| } | |
| - (UIView *)findCropView:(UIView *)view { | |
| CGFloat width = [[UIScreen mainScreen] bounds].size.width; | |
| CGSize size = [view frame].size; | |
| if (size.width == width && size.height == width) { | |
| return view; | |
| } | |
| for (UIView *subview in [view subviews]) { | |
| UIView *view = [self findCropView:subview]; | |
| if (view) { | |
| return view; | |
| } | |
| } | |
| return nil; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment