ios - 将 UIImagePickerController 修复为纵向

标签 ios

我以这种方式将 UIImagePickerController 添加到我的 View 中:

- (IBAction)TakeCameraShot:(id)sender{

if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeCamera])
{

    // Given by default your orientation is in landscaperight already
    while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;


   /* picker.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    picker.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCurrentContext;*/
    [self presentViewController:picker animated:YES completion:nil];

   /* picker.view.layer.cornerRadius = 150;
    picker.view.clipsToBounds = YES;
    picker.view.frame = CGRectMake(12, 60, 296, 290);*/

}else{
    NSLog(@"Camera is not available");
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Important message" message:@"Unfortunately the camera is not available on your device." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert1 show];
}
}

我试图用这段代码防止它被旋转成横向

// Given by default your orientation is in landscaperight already
    while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

提供的堆栈溢出的答案还说,只有当 UIviewController 已经设置为仅纵向时它才会工作,所以这就是我在一般应用程序设置(您选择 ipad、iphone 或 universal)中所做的。但这不起作用。

最佳答案

此代码将在我使用过的 Ipad 上运行,但您需要添加此代码

while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

在呈现 UIImagePickerController 之后 ---

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

     while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
        [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [self presentViewController:picker animated:YES completion:nil];

while ([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
     [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

这也适用于 ipod,但在 ipod 上它仍然会给你旋转的图像,如果你想在 ipod 中始终使用 potrait 图像,你需要像这样改变它们的方向

UIImageOrientationRight -- 此方向是当相机处于纵向时单击图像时的方向 UIImageOrientationUp——当相机处于横向左侧时

UIImageOrientationDown -- 这是相机横向正确的时候

UIImageOrientationLeft -- 这是相机上下颠倒的时候

现在除了将图像方向更改为 UIImageOrientationRight 之外的所有其他三种情况下。

这可以通过检查即将到来的图像的方向并应用新的方向来完成。

关于ios - 将 UIImagePickerController 修复为纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18854917/

相关文章:

ios - 当 iPhone 处于锁定状态的后台模式时,NSDATA 没有写入文档目录?

android - 在错误地创建应用程序而不是库后,像在 iOS 中一样在 Android Studio 中创建目标

iphone - 如何使用 PhoneGap 在 iPhone 应用程序中添加 UITableview?

iphone - UIPanGestureRecognizer - View 在拖动时消失

iOS - 如何使用 SQLite.swift 进行变音符号不敏感搜索?

ios - 涉及多个 View Controller ,如何将值发送回启动 View Controller ?

objective-c - iOS - 流式传输大文件以供上传(使用 NSData 分配太大文件时应用程序崩溃)

html - 从 NSString 中提取一个 html 标签和属性

iphone - 在 Objective-C 中将 TypeDef 作为参数

ios - 在 IntelliJ Idea(在 Mac 中)中使用文件名搜索/查找文件?