UIImagePicker allowsEditing stuck in center

后端 未结 6 1896
后悔当初
后悔当初 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:14

    I know, this is not a good solution, but it works.

    I tested on iOS8+iPhone5, iOS9+iPhone6sPlus, iOS10+iPhone6, iOS10+iPhone6sPlus.

    CAUTION: PLImageScrollView and PLCropOverlayCropView are UNDOCUMENTED classes.

    - (void)showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType {
        UIImagePickerController *imagePickerController = [UIImagePickerController new];
        imagePickerController.sourceType = sourceType;
        imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage];
        imagePickerController.allowsEditing = YES;
        imagePickerController.delegate = self;
        [self presentViewController:imagePickerController animated:YES completion:^{
            [self fxxxImagePickerController:imagePickerController];
        }];
    }
    
    - (void)fxxxImagePickerController:(UIImagePickerController *)imagePickerController {
        if (!imagePickerController
            || !imagePickerController.allowsEditing
            || imagePickerController.sourceType != UIImagePickerControllerSourceTypeCamera) {
            return;
        }
    
        // !!!: UNDOCUMENTED CLASS
        Class ScrollViewClass = NSClassFromString(@"PLImageScrollView");
        Class CropViewClass = NSClassFromString(@"PLCropOverlayCropView");
    
        [imagePickerController.view eachSubview:^BOOL(UIView *subview, NSInteger depth) {
            if ([subview isKindOfClass:CropViewClass]) {
                // 0. crop rect position
                subview.frame = subview.superview.bounds;
            }
            else if ([subview isKindOfClass:[UIScrollView class]]
                && [subview isKindOfClass:ScrollViewClass]) {
                BOOL isNewImageScrollView = !self->_imageScrollView;
                self->_imageScrollView = (UIScrollView *)subview;
                // 1. enable scrolling
                CGSize size = self->_imageScrollView.frame.size;
                CGFloat inset = ABS(size.width - size.height) / 2;
                self->_imageScrollView.contentInset = UIEdgeInsetsMake(inset, 0, inset, 0);
                // 2. centering image by default
                if (isNewImageScrollView) {
                    CGSize contentSize = self->_imageScrollView.contentSize;
                    if (contentSize.height > contentSize.width) {
                        CGFloat offset = round((contentSize.height - contentSize.width) / 2 - inset);
                        self->_imageScrollView.contentOffset = CGPointMake(self->_imageScrollView.contentOffset.x, offset);
                    }
                }
            }
            return YES;
        }];
    
        // prevent re-layout, maybe not necessary
        @weakify(self, imagePickerController);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            @strongify(self, imagePickerController);
            [self fxxxImagePickerController:imagePickerController];
        });
    }
    

    EDIT: The eachSubview: method traverses all the subviews tree.

提交回复
热议问题