2012-09-27 38 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