Objective c 在iPad上以纵向模式启动图像选择器

Objective c 在iPad上以纵向模式启动图像选择器,objective-c,ios,xcode,ipad,Objective C,Ios,Xcode,Ipad,我正在使用以下代码在我的iPad应用程序(使用Cocos2D)中启动图像选择器: 我希望它一直以纵向模式启动,但它总是这样启动: 图像以纵向显示,图像选择器UI处于横向模式。但当我拍摄图像时,它会顺时针旋转90度 我希望图像选择器用户界面和拍摄的图像都处于纵向模式。我怎样才能解决这个问题 您需要做三件事: 告诉仅向其显示imagePicker的viewController 支持纵向定位 告诉imagePicker不要旋转实时相机馈送 取消旋转捕获的图像 imagePicker直接接收设备方向

我正在使用以下代码在我的iPad应用程序(使用Cocos2D)中启动图像选择器:

我希望它一直以纵向模式启动,但它总是这样启动:

图像以纵向显示,图像选择器UI处于横向模式。但当我拍摄图像时,它会顺时针旋转90度


我希望图像选择器用户界面和拍摄的图像都处于纵向模式。我怎样才能解决这个问题

您需要做三件事:

  • 告诉仅向其显示imagePicker的viewController 支持纵向定位
  • 告诉imagePicker不要旋转实时相机馈送
  • 取消旋转捕获的图像
  • imagePicker直接接收设备方向更改的通知。要防止实时相机馈送与设备一起旋转,您可以通过以下方式停止它接收这些通知:告诉UIDevice结束在显示imagePicker后生成方向通知:

    while ([currentDevice isGeneratingDeviceOrientationNotifications])
            [currentDevice endGeneratingDeviceOrientationNotifications];
    
    之所以将其放入循环中,是因为imagePicker开始生成DeviceOrientationNotifications,然后在其被解除时结束,使正常设备上的通知计数从1变为2变为1

    解除imagePicker后,您可以调用:

    while (![currentDevice isGeneratingDeviceOrientationNotifications])
            [currentDevice beginGeneratingDeviceOrientationNotifications];
    
    以便您的ViewController可以继续接收方向更改通知

    不幸的是,即使关闭此选项,图像仍将在拍摄时以相机的正确图像方向保存,因此在保存图像或对其执行任何操作之前,您必须通过手动反向消除应用的方向变换:

    -(UIImage *)turnMeAround:(UIImage *)image{
        CGAffineTransform transform = CGAffineTransformIdentity;
        CGFloat scale = [[UIScreen mainScreen] scale]; //retina
        CGSize size = CGSizeMake(image.size.width*scale,image.size.height*scale);
        switch (image.imageOrientation) {
            case UIImageOrientationUp:
                return image;
            case UIImageOrientationDown:
                size = CGSizeMake(size.height,size.width);
                transform = CGAffineTransformRotate(transform, M_PI);
                break;
            case UIImageOrientationLeft:
                transform = CGAffineTransformRotate(transform, -M_PI_2);
                break;
            case UIImageOrientationRight:
                transform = CGAffineTransformRotate(transform, M_PI_2);
                break;
        }
        CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, CGImageGetBitsPerComponent(image.CGImage), 0, CGImageGetColorSpace(image.CGImage), CGImageGetBitmapInfo(image.CGImage));
        CGContextConcatCTM(context, transform);
        CGContextDrawImage(context, CGRectMake(0,0,size.width,size.height), image.CGImage);
        CGImageRef ref = CGBitmapContextCreateImage(context);
        UIImage *upsideRight = [UIImage imageWithCGImage:ref];
        CGContextRelease(context);
        CGImageRelease(ref);
        return upsideRight;
    }
    

    非常感谢大家的帮助,但对我有效的方法如下:

    所以我发现罪魁祸首是Xcode的Cocos2D模板。它在RootViewController中生成了以下代码:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
    //
    // There are 2 ways to support auto-rotation:
    //  - The OpenGL / cocos2d way
    //     - Faster, but doesn't rotate the UIKit objects
    //  - The ViewController way
    //    - A bit slower, but the UiKit objects are placed in the right place
    //
    
    #if GAME_AUTOROTATION==kGameAutorotationNone
    //
    // EAGLView won't be autorotated.
    // Since this method should return YES in at least 1 orientation, 
    // we return YES only in the Portrait orientation
    //
    return ( interfaceOrientation == UIInterfaceOrientationPortrait );
    
    #elif GAME_AUTOROTATION==kGameAutorotationCCDirector
    //
    // EAGLView will be rotated by cocos2d
    //
    // Sample: Autorotate only in landscape mode
    //
    if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
        [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
    } else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        [[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
    }
    
    // Since this method should return YES in at least 1 orientation, 
    // we return YES only in the Portrait orientation
    return ( interfaceOrientation == UIInterfaceOrientationPortrait );
    
    #elif GAME_AUTOROTATION == kGameAutorotationUIViewController
    //
    // EAGLView will be rotated by the UIViewController
    //
    // Sample: Autorotate only in landscpe mode
    //
    // return YES for the supported orientations
    
    return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
    
    #else
    #error Unknown value in GAME_AUTOROTATION
    
    #endif // GAME_AUTOROTATION
    
    
    // Shold not happen
    return NO;
    }
    
    在上述代码中,我更改了以下内容:

    return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
    
    为此:

    return ( UIInterfaceOrientationIsPortrait( interfaceOrientation ) );
    

    这就解决了问题。

    您是否以任何方式修改了picker.view.transform?不,我没有。但是我在我的项目中使用Cocos2D模板。你认为这可能在某种程度上导致了问题?
    return ( UIInterfaceOrientationIsPortrait( interfaceOrientation ) );