UIImagePicker allowsEditing stuck in center

后端 未结 6 1897
后悔当初
后悔当初 2020-11-29 02:47

I have a UIImagePicker that works perfect for a type of UIImagePickerControllerSourceTypePhotoLibrary, but when I use UIImagePickerControllerSourceTypeCamera, the editing bo

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 03:32

    Stupid answer
    Reset contentInset of scrollview

    #import 
    
    @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 (PLUS)
    
    // 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 xox
    - (void)fixCannotMoveEditingBox {
        if (!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, -1);
            }
        }
    
        __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
    

    then call it

    [imagePickerController fixCannotMoveEditingBox];
    

提交回复
热议问题