2013-12-08 2 views
6

Ho app che registra un video.AVAssetTrack preferredTransform restituisce sempre landscape

Per gestire la rotazione del telefono Ho il seguente codice:

// called on phone rotation 
    AVCaptureConnection *previewLayerConnection = [[self previewLayer] connection]; 
    if ([previewLayerConnection isVideoOrientationSupported]) { 
     [previewLayerConnection setVideoOrientation:[self getVideoOrientation]]; 
    } 

e getVideoOrientation funzione:

- (AVCaptureVideoOrientation) getVideoOrientation { 
    UIInterfaceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 
    AVCaptureVideoOrientation newOrientation = AVCaptureVideoOrientationPortrait; 
    switch (deviceOrientation) { 
     case UIInterfaceOrientationPortrait: 
      NSLog(@"UIInterfaceOrientationPortrait"); 
      newOrientation = AVCaptureVideoOrientationPortrait; 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
      NSLog(@"UIInterfaceOrientationLandscapeRight"); 
      newOrientation = AVCaptureVideoOrientationLandscapeLeft; 
      break; 
     case UIInterfaceOrientationLandscapeRight: 
      NSLog(@"UIInterfaceOrientationLandscapeLeft"); 
      newOrientation = AVCaptureVideoOrientationLandscapeRight; 
      break; 
     default: 
      NSLog(@"default"); 
      newOrientation = AVCaptureVideoOrientationPortrait; 
      break; 
    } 

    return newOrientation; 
} 

Questa parte della applicazione funziona correttamente (vedo il video come avrei dovuto su qualsiasi orientamento del dispositivo). Ma quando provo a fare una miniatura (o riprodurre video) ho dei problemi.

Come ho letto in altre questioni che faccio la seguente: per ogni traccia

 AVAssetTrack* videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
     CGAffineTransform txf  = [videoTrack preferredTransform]; 
     CGFloat videoAngleInDegree = RadiansToDegrees(atan2(txf.b, txf.a)); 

     if (txf.a == 0 && txf.b == 1.0 && txf.c == -1.0 && txf.d == 0) { 
      thumbOrientation = UIImageOrientationLeft; 
     } 
     if (txf.a == 0 && txf.b == -1.0 && txf.c == 1.0 && txf.d == 0) { 
      thumbOrientation = UIImageOrientationRight; 
     } 
     if (txf.a == 1.0 && txf.b == 0 && txf.c == 0 && txf.d == 1.0) { 
      thumbOrientation = UIImageOrientationUp; 
     } 
     if (txf.a == -1.0 && txf.b == 0 && txf.c == 0 && txf.d == -1.0) { 
      thumbOrientation = UIImageOrientationDown; 
     } 

     UIImage *image = [UIImage imageWithCGImage:im scale:1.0 orientation:thumbOrientation]; 

ho 2 file di esempio: 1 - paesaggio giuste, 2 - paesaggio di sinistra. Mi aspetto che abbiano diversi orientamenti nel codice sopra, ma inaspettatamente hanno lo stesso (e videoAngleInDegree è lo stesso per entrambi).

Esistono soluzioni alternative?

risposta

1

Sei sicuro che funzioni correttamente? Il numero getVideoOrientation sembra errato: AVCapture e UIDevice hanno significati opposti per il panorama a sinistra ea destra.

Ecco una corretta implementazione da this question - controlla la discussione e controlla se è utile.

- (void)deviceOrientationDidChange{ 

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation]; 

    AVCaptureVideoOrientation newOrientation; 

    if (deviceOrientation == UIDeviceOrientationPortrait){ 
     NSLog(@"deviceOrientationDidChange - Portrait"); 
     newOrientation = AVCaptureVideoOrientationPortrait; 
    } 
    else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown){ 
     NSLog(@"deviceOrientationDidChange - UpsideDown"); 
     newOrientation = AVCaptureVideoOrientationPortraitUpsideDown; 
    } 

    // AVCapture and UIDevice have opposite meanings for landscape left and right (AVCapture orientation is the same as UIInterfaceOrientation) 
    else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){ 
     NSLog(@"deviceOrientationDidChange - LandscapeLeft"); 
     newOrientation = AVCaptureVideoOrientationLandscapeRight; 
    } 
    else if (deviceOrientation == UIDeviceOrientationLandscapeRight){ 
     NSLog(@"deviceOrientationDidChange - LandscapeRight"); 
     newOrientation = AVCaptureVideoOrientationLandscapeLeft; 
    } 

    else if (deviceOrientation == UIDeviceOrientationUnknown){ 
     NSLog(@"deviceOrientationDidChange - Unknown "); 
     newOrientation = AVCaptureVideoOrientationPortrait; 
    } 

    else{ 
     NSLog(@"deviceOrientationDidChange - Face Up or Down"); 
     newOrientation = AVCaptureVideoOrientationPortrait; 
    } 

    [self setOrientation:newOrientation]; 
}