2012-09-27 35 views
34

我有一个UIImagePicker,适用于UIImagePickerControllerSourceTypePhotoLibrary类型的完美,但是当我使用UIImagePickerControllerSourceTypeCamera时,编辑框不能从图像的中心移动。因此,如果图像高于宽度,则用户无法将编辑框移动到图像的上方。UIImagePicker允许编辑卡在中心

任何人都知道为什么会这样?它只发生在来源是相机而不是图书馆的情况下。

编辑:一些代码!!!

if (actionSheet.tag == 2) { 
    if (buttonIndex == 0) { // Camera 
     // Check for camera 
     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) { 
      // Create image picker controller 
      UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 

      // Set source to the camera 
      imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
      imagePicker.allowsEditing = YES; 

      // Delegate is self 
      imagePicker.delegate = self; 

      // Show image picker 
      [self presentViewController:imagePicker 
           animated:YES 
          completion:^(void) { 
          }]; 
     } 
    } 
    else if (buttonIndex == 1) { // Photo Library 
     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] == YES) { 
      // Create image picker controller 
      UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 

      // Set source to the camera 
      imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
      imagePicker.allowsEditing = YES; 

      // Delegate is self 
      imagePicker.delegate = self; 

      // Show image picker 
      [self presentViewController:imagePicker 
           animated:YES 


          completion:^(void) { 
           }]; 
      } 
} 

所以你可以看到,我展示他们完全一样的,但相机编辑的作用比照片库编辑不同。

回答

47

貌似这种行为只是在iOS 6中的一个错误......基本上你不能移动的编辑框,它总是弹回中间,除非你放大在一点。希望他们尽快解决。

+6

我认为这仍然是iOS 7 –

+9

aaa中的一个错误,并且仍然是iOS 8中的一个错误。:( –

+9

即使是iOS 8.1也存在相同的错误 – Adnan

0

这是图像选取器控制器的默认行为,您不能更改它。唯一的其他选择是创建您自己的裁剪实用程序。看看下面的链接中的示例:

https://github.com/ardalahmet/SSPhotoCropperViewController

+2

但是,当你从相册选择它的作品中添加info.plist中的条目的解决办法..不合理。为什么你可以把它从一个专辑,而不是相机 – Eric

+0

@Eric你可能需要发布一些代码 – Vikings

+0

它有我用来显示它的代码 – Eric

0

我知道,这不是一个好的解决方案,但它的工作原理。

我在iOS8 + iPhone5,iOS9 + iPhone6sPlus,iOS10 + iPhone6,iOS10 + iPhone6sPlus上测试过。

注意PLImageScrollViewPLCropOverlayCropViewUNDOCUMENTED类。

- (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]; 
    }); 
} 

编辑:该eachSubview:方法遍历所有的子视图树。

-1

上解决了它与“查看基于控制器的状态栏外观”设置为NO