iOS 系统相机拍照后图片无法拖拽问题的解决

问题:使用系统相机拍照,并允许编辑allowsEditing = YES,在图片编辑界面可以对图片进行缩放但无法拖拽

大牛给出了解决方案,我用着是好使
https://stackoverflow.com/questions/12630155/uiimagepicker-allowsediting-stuck-in-center/53440254#53440254
这是swift版的代码,如果使用oc开发可以桥联也可以用下面oc代码:

创建UIImagePickerController分类

// .h文件
#import 

@interface UIImagePickerController (LLImagePickerVC)
- (void)fixCannotMoveEditingBox;
@end
#import "UIImagePickerController+LLImagePickerVC.h"

@implementation UIImagePickerController (LLImagePickerVC)

- (UIScrollView *)findScrollViewFrom:(UIView *)view {
    if ([view isKindOfClass:UIScrollView.class]) {
        return (UIScrollView *)view;
    }
    
    for (UIView *tempView in view.subviews) {
        UIView *view = [self findScrollViewFrom:tempView];
        if ([view isKindOfClass:UIScrollView.class]) {
            return (UIScrollView *)view;
        }
    }
    
    return nil;
}

- (UIView *)findCropViewFrom:(UIView *)view {
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGSize size = view.bounds.size;
    if (screenWidth == size.height && screenWidth == size.height) {
        return view;
    }
    
    for (UIView *tempView in view.subviews) {
        UIView *view = [self findCropViewFrom:tempView];
        if (view) return view;
    }
    
    return nil;
}

- (void)fixCannotMoveEditingBox {
    UIView *cropView = [self findCropViewFrom:self.view];
    UIScrollView *scrollView = [self findScrollViewFrom:self.view];
    if (cropView && scrollView && scrollView.contentOffset.y == 0) {
        CGFloat top = CGRectGetMinY(cropView.frame) + self.view.safeAreaInsets.top;
        CGFloat bottom = scrollView.frame.size.height - cropView.frame.size.height - top;
        scrollView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
        
        CGFloat offset = 0;
        if (scrollView.contentSize.height > scrollView.contentSize.width) {
            offset = 0.5 * (scrollView.contentSize.height - scrollView.contentSize.width);
        }
        scrollView.contentOffset = CGPointMake(0, -top + offset);
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self fixCannotMoveEditingBox];
    });
}

@end
// 使用
UIImagePickerController *imagePickerController = [UIImagePickerController new];
[imagePickerController fixCannotMoveEditingBox];
// 其他配置
// ...
[self presentViewController:imagePickerController animated:YES completion:nil];

你可能感兴趣的:(iOS 系统相机拍照后图片无法拖拽问题的解决)