2012-12-18 3 views
0

Работа с iOS-приложением, и я пытаюсь выяснить, как получить фактическую ориентацию устройства, даже если приложение зафиксировано в портретном режиме. В настоящий момент все проверки ориентации дают мне портрет. Это то, что я проверил.получить фактическую ориентацию устройства

CGRect frame = [[UIScreen mainScreen] applicationFrame]; 
UIInterfaceOrientation orientation2 = [[UIApplication sharedApplication] statusBarOrientation]; 
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; 

В основном я хочу знать ориентацию фото берется в России, но если выбора камеры погрузили в портретном и телефон превращается в пейзаж, после чего результат ориентации фотографии является портрет. Я надеялся, что смогу проверить фактическую ориентацию устройства в момент съемки фотографии и соответствующим образом преобразовать ее. Любые идеи о том, как подойти к этому?

Редактировать: Я собираюсь изучить использование гироскопа, чтобы это сообщило, как это происходит.

редактировать: жаль, что я должен отметить, что сам телефон находится в фиксированном портрете orientation.but есть способ обойти, что посмотреть, что это на самом деле

ответ

0

Pass захваченного изображения этой функцию & возвращает изображение с ориентацией, которую вы его захватили.

- (UIImage *)fixOrientation:(UIImage *)myImage 
{ 

    // No-op if the orientation is already correct 
    if (myImage.imageOrientation == UIImageOrientationUp) return myImage; 

    // We need to calculate the proper transformation to make the image upright. 
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 
    CGAffineTransform transform = CGAffineTransformIdentity; 

    switch (myImage.imageOrientation) { 
     case UIImageOrientationDown: 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformTranslate(transform, myImage.size.width, myImage.size.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
      transform = CGAffineTransformTranslate(transform, myImage.size.width, 0); 
      transform = CGAffineTransformRotate(transform, M_PI_2); 
      break; 

     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      transform = CGAffineTransformTranslate(transform, 0, myImage.size.height); 
      transform = CGAffineTransformRotate(transform, -M_PI_2); 
      break; 
     case UIImageOrientationUp: 
     case UIImageOrientationUpMirrored: 
      break; 
    } 

    switch (myImage.imageOrientation) { 
     case UIImageOrientationUpMirrored: 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformTranslate(transform, myImage.size.width, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 

     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRightMirrored: 
      transform = CGAffineTransformTranslate(transform, myImage.size.height, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 
     case UIImageOrientationUp: 
     case UIImageOrientationDown: 
     case UIImageOrientationLeft: 
     case UIImageOrientationRight: 
      break; 
    } 

    // Now we draw the underlying CGImage into a new context, applying the transform 
    // calculated above. 
    CGContextRef ctx = CGBitmapContextCreate(NULL, myImage.size.width, myImage.size.height, 
              CGImageGetBitsPerComponent(myImage.CGImage), 0, 
              CGImageGetColorSpace(myImage.CGImage), 
              CGImageGetBitmapInfo(myImage.CGImage)); 
    CGContextConcatCTM(ctx, transform); 
    switch (myImage.imageOrientation) { 
     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      // Grr... 
      CGContextDrawImage(ctx, CGRectMake(0,0,myImage.size.height,myImage.size.width), myImage.CGImage); 
      break; 

     default: 
      CGContextDrawImage(ctx, CGRectMake(0,0,myImage.size.width,myImage.size.height), myImage.CGImage); 
      break; 
    } 

    // And now we just create a new UIImage from the drawing context 
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
    UIImage *img = [UIImage imageWithCGImage:cgimg]; 
    CGContextRelease(ctx); 
    CGImageRelease(cgimg); 
    return img; 
} 
+0

спасибо, но ориентация изображения myImage всегда будет UIImageOrientationUp как то, что устройство считает, что это в том, когда приложение находится в режиме фиксированного портрета. Я уже пробовал эту функцию, поэтому я очень уверен, что она всегда дает результат UIImageOrientationUp – glogic

0

Использование «s orientation свойство

[[UIDevice currentDevice] orientation]; 

возвращает вам физическую ориентацию устройства.

ref

+0

боюсь, что я тоже это сделал. Ориентация UIDeviceOrientation = [[UIDevice currentDevice]); дает мне UIDeviceOrientationPortrait, когда я держу телефон в ландшафте при съемке – glogic

Смежные вопросы