2014-02-11 106 views
9

我正在使用UIImagePickerController和自定義疊加層構建應用程序。比較兩幅圖像(圖像前和圖像後)的作用是什麼。在拍攝照片後,我正在使用前一張圖片的自定義覆蓋圖(請參閱附圖)。UIImagePickerController相機視圖尺寸問題

iPhone 5 - ios7 enter image description here

iPhone 4 - iOS7(如果要拍攝的圖像) enter image description here

iPhone 4 - 的iOS 7(後拍攝照片) enter image description here

見iPhone 4和iPhone 5相機視圖之間的大小差異。

應用程序適用於iPhone 5的屏幕尺寸(iOS 6和ios7)。但iPhone 4/4s的屏幕尺寸,只有在iOS6下才能正常工作。問題是iPhone 4/4s(僅限ios7),相機視圖需要全屏顯示。這個意思是,你可以注意到 iPhone 5相機的視圖大小〜320 * 427(iOS 6和iOS 7) iPhone 4相機的視圖大小〜320 * 427(iOS 6) BUT iPhone 4相機的視圖大小〜320 * 480 (IOS 7)。

圖像拍攝後,它適合於320 * 427的實際尺寸。由於這個問題,我無法將圖像與iPhone 4 iOS7上的相機視圖對齊(因爲它的尖叫聲爲320 * 480)。

有沒有人面臨這個奇怪的問題。我嘗試了幾乎所有的東西,但沒有運氣。請任何想法?

這是我的片段代碼,用於在照片疊加之前加載自定義照相機視圖。

- (void)loadCameraWithImage 
{ 
    if (!isLoadedOnce) 
    { 
    isLoadedOnce = YES; 
      UIImagePickerController *cameraView = [[UIImagePickerController alloc] init]; 
      cameraView.sourceType = UIImagePickerControllerSourceTypeCamera; 
      cameraView.wantsFullScreenLayout = NO; 

     if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { 
     [self setEdgesForExtendedLayout:UIRectEdgeNone]; 
     } 


    // crop before image 

    UIImage *imgTmpCropped = [self imageByCropping:imgBefore toRect:CGRectMake(0, 0, imgBefore.size.width/2, imgBefore.size.height)]; 
    UIImage *overleyImage = [[UIImage alloc] initWithCGImage: imgTmpCropped.CGImage 
                scale: [UIScreen mainScreen].scale 
              orientation: UIImageOrientationDownMirrored]; 

    UIImageView *crosshairView; 
    UIImageView *beforeView; 

    CGFloat screenHieght = [UIScreen mainScreen].bounds.size.height; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ 
     //overleyImage = [UIImage imageNamed:@"overlay_ipad.png"]; 
     crosshairView = [[UIImageView alloc] initWithImage:overleyImage]; 
     crosshairView.frame = CGRectMake(0, 0, 768, 1024); 
     [crosshairView setUserInteractionEnabled:YES]; 
     crosshairView.backgroundColor = [UIColor clearColor]; 

     beforeView = [[UIImageView alloc] initWithImage:overleyImage]; 
     beforeView.frame = CGRectMake(0, 0, 384, 1024); 
     beforeView.alpha = 0.5; 
     beforeView.contentMode = UIViewContentModeScaleAspectFill; 
    } 

    else { 
     //overleyImage = [UIImage imageNamed:@"overleyImageAfter.png"]; 
     crosshairView = [[UIImageView alloc] init]; 
     beforeView = [[UIImageView alloc] initWithImage:overleyImage]; 

     if(screenHieght>500){ 
      crosshairView.frame = CGRectMake(0, 60, 320, 480); 
      beforeView.frame = CGRectMake(0, 70, 160, 427); 
     } 
     else{ 

      if([[[UIDevice currentDevice] systemVersion] floatValue] <7.0){ 
       crosshairView.frame = CGRectMake(0, 0, 320, 480); 
       beforeView.frame = CGRectMake(0, 0, 160, 427); 
      } 
      else{ 
       crosshairView.frame = CGRectMake(0, 0, 320, 480); 
       beforeView.frame = CGRectMake(0, 0, 160, 480); 
      } 

     } 

     [crosshairView setUserInteractionEnabled:YES]; 
     crosshairView.backgroundColor = [UIColor clearColor]; 
     beforeView.alpha = 0.5; 
     beforeView.contentMode = UIViewContentModeScaleToFill; 
    } 


    //[crosshairView addSubview:beforeView]; 

    //set our custom overlay view 
    cameraView.cameraOverlayView = beforeView; 
    cameraView.delegate = (id)self; 
    cameraView.showsCameraControls = YES; 
    cameraView.navigationBarHidden = YES; 
    cameraView.toolbarHidden = YES; 
    [cameraView setHidesBottomBarWhenPushed:YES]; 


    [self.view.window.rootViewController presentViewController:cameraView animated:YES completion:nil]; 

    isLoadedOnce = NO; 
} 

}

+0

您是否曾經能夠爲此找到解決方案?我面臨類似的問題。 –

+0

我可以解釋這個問題。但你有沒有找到解決辦法? 隨着iPhone 4(的iOS 7): 圖像尺寸:{1936年,2592}這是你使用 「UIImagePickerControllerOriginalImage」 但 視圖/窗口尺寸是挑選的UIImage從信息詞典:{320,416}規模:2問題幾乎是6.05倍,高度是6.23倍。所以當你看到這個圖像時,它會被扭曲。 如果你發現了一些東西,你可以請分享?乾杯 –

+0

可悲的是沒有解決方案。在我的項目中,我必須遵循這個限制 – sajaz

回答

9

TL; DR

相機的預覽具有在每個設備中相同的寬高比(4:3),屏幕沒有。假設預覽將具有該特定的高寬比,並且它將被放置在屏幕上的特定位置。在該區域內繪製覆蓋圖。

更長

我們面臨着同樣的問題。 (我們的用例是使用與卡片尺寸相同的疊加信用卡拍攝照片)。主要問題是相機預覽的比例在每個設備中都是相同的(4:3),但不同的手機有不同的屏幕比例(例如iPhone 4s和iPhone 5),因此預覽需要進行不同的配合,覆蓋和裁剪非常困難。

我們的解決方案是(代碼是有點混亂和哈克,對不起):

// Adjust camera preview to be a little bit more centered instead of adjusted to the top 
    CGSize screenSize = [[UIScreen mainScreen] bounds].size; 
    float cameraAspectRatio = 4.0/3.0; 
    float imageHeight = screenSize.width * cameraAspectRatio; 
    float verticalAdjustment; 
    if (screenSize.height - imageHeight <= 54.0f) { 
     verticalAdjustment = 0; 
    } else { 
     verticalAdjustment = (screenSize.height - imageHeight)/2.0f; 
     verticalAdjustment /= 2.0f; // A little bit upper than centered 
    } 
    CGAffineTransform transform = self.cameraController.cameraViewTransform; 
    transform.ty += verticalAdjustment; 
    self.cameraController.cameraViewTransform = transform; 

    CGRect previewFrame = CGRectMake(0, verticalAdjustment, screenSize.width, imageHeight); 
    CardPhotoView *overlayView = [[CardPhotoView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height) widthPercentageOfCamera:self.widthPercentageOfCamera previewFrame:previewFrame]; 
    self.overlayView = overlayView; 

    self.overlayView.delegate = self; 
    self.overlayView.useViewport = YES; 
    [self.overlayView setCameraReady:NO]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cameraIsReady:) name:AVCaptureSessionDidStartRunningNotification object:nil]; 

    self.cameraController.showsCameraControls = NO; 
    self.cameraController.navigationBarHidden = YES; 
    self.cameraController.toolbarHidden = YES; 
    self.cameraController.cameraOverlayView = self.overlayView; 

    [self presentViewController:self.cameraController animated:NO completion:nil]; 

說明

self.cameraControllerUIImagePickerControllerCardPhotoView是繪製疊加層的視圖子類,它需要預覽幀才能準確知道預覽的位置(某些設備的頂部和底部會有黑條,有些則不會)。它還需要一個寬度百分比來添加一些填充到裁剪窗口。 另外,我們隱藏了所有的默認相機控制,我們已經實現了一些按鈕,將爲我們做的工作。這些按鈕添加在CardPhotoView類中,並且它們是在考慮預覽幀的位置的情況下繪製的。