iphone:从相机捕获的图像改变方向

2024-01-17

我制作了一个 iPhone 应用程序来从相机捕获图像并在下一个视图中设置该图像。但问题是图像被旋转了。即风景图像变成肖像,肖像图像变成风景。 我参考了很多代码但无法得到解决方案。

我的代码是:

- (void)btnCapturePressed
{
 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {

            picker=[[UIImagePickerController alloc] init];
            picker.delegate=self;

            [picker setAllowsEditing:YES];

            picker.sourceType=UIImagePickerControllerSourceTypeCamera;
            //[self.navigationController pushViewController:(UIViewController *)ipc animated:YES];
            [self presentModalViewController:picker animated:YES];
            [picker release];
      }
}

-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo :(NSDictionary *)info
{
           UIImage *imageToScale=[info objectForKey:UIImagePickerControllerOriginalImage];

            imgView = [[UIImageView alloc] initWithImage:imageToScale];

            [picker presentModalViewController:cropper animated:YES];
}

我还提到了link https://stackoverflow.com/questions/9324130/iphoneimage-captured-from-camera-rotate-90-degree-automatically。遇到同样的问题,但找不到解决方案。

请帮我。

Thanks.


因此,在拍摄图像时,存储设备的方向并将其作为参数传递给下面的方法 这里只需给方法起任何名称并传递参数orientation

switch (orientation) {
            case UIDeviceOrientationPortrait:
                [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(0.))];
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(180.))];
                break;
            case UIDeviceOrientationLandscapeLeft:
                [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(90.))];
                break;
            case UIDeviceOrientationLandscapeRight:
                [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.))];
                break;
            case UIDeviceOrientationFaceUp:
            case UIDeviceOrientationFaceDown:
            default:
                break; // leave the layer in its last known orientation
        }

和我在这里使用的宏DegreesToRadians()如下

static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};

这肯定会有效。

快乐编码:)

EDIT

如果上面的代码不能很好地工作,那么使用这个

@interface UIImage (RotationMethods)
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
@end

@implementation UIImage (RotationMethods)

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees 
{   
    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
    rotatedViewBox.transform = t;
    CGSize rotatedSize = rotatedViewBox.frame.size;

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, DegreesToRadians(degrees));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}
@end

然后调用上面的函数如下

switch (orientation) {
        case UIDeviceOrientationPortrait:
            image = [image imageRotatedByDegrees:0];
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            image = [image imageRotatedByDegrees:180];
            break;
        case UIDeviceOrientationLandscapeLeft:
            image = [image imageRotatedByDegrees:-90];
            break;
        case UIDeviceOrientationLandscapeRight:
            image = [image imageRotatedByDegrees:90];
            break;
        case UIDeviceOrientationFaceUp:
        case UIDeviceOrientationFaceDown:
        default:
            break; // leave the layer in its last known orientation
    }   

如果图像的方向不符合要求,则将 90 添加到上述所有值imageRotatedByDegrees的参数(即如果它是 0 那么它将是 0+90)或根据您的要求。

EDIT 1

UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iphone:从相机捕获的图像改变方向 的相关文章

随机推荐

  • 使用 JWT OAuth 2.0 令牌的 Cordova 应用程序中的 Azure MFA

    我正在开发一个 Cordova 应用程序 该应用程序 到目前为止 使用密码授予从 Azure 中的 Microsoft 标准 OAuth 提供程序检索 JWT https login microsoftonline com tenant o
  • 使用 bootstrap-vue 导航栏 item-dropdown 更改文本颜色

    我在用Bootstrap Vue编写网页 但我无法更改网页上的文本颜色Bootstrap navbar 尤其是b nav item dropdown标签 我尝试过使用 span class text dark span
  • 如何获取我的项目路径? [复制]

    这个问题在这里已经有答案了 可能的重复 使用 C 获取我的 exe 的路径 https stackoverflow com questions 3991933 get path for my exe using c sharp 你好我有一个
  • 如何使用 terraform 将 ssh 密钥添加到 GCP 实例?

    因此 我有一个在 Google Cloud Platform 中创建实例的 terraform 脚本 我希望能够让我的 terraform 脚本也将我的 ssh 密钥添加到我创建的实例中 以便我可以通过 ssh 来配置它们 这是我当前的 t
  • 使用UTF-16LE编码读取csv的快速方法

    我正在处理使用 UTF 16LE 编码的 csv 文件 此方法可以读取文件 但与 read csv 相比 read csv 非常慢 read csv2 path dec skip 1 header T fileEncoding UTF 16
  • Scrollviewer 和 SIP 问题 (WP7.5 Mango)

    我正在编写一个包含注册表单的应用程序 该表单包含多个文本输入框 因此使用 ScrollViewer 将它们全部显示在一页上 以下是我正在使用的 XAML 代码的精简示例
  • 循环遍历sed的结果

    我在循环访问 sed 返回的存储在变量中的行时遇到问题 目前看起来有点像这样 lines sed rne timestamplastupload timestampnow p var log test log for line in lin
  • ASP.Net Core 中的 Hangfire:简单的重复作业不会刷新其动态内容

    我正在尝试在 ASP Net Core 1 1 中的 Web 应用程序上实现 cron 任务 我决定选择Hangfire https www hangfire io 图书馆 为了检查我的安装和配置是否正常工作 我刚刚编写了一个非常简单的重复
  • 如何在 android 单元测试中创建位置?

    我想使用 JUnit 4 在 android 单元测试中创建一个位置 和 位置 loc 新位置 loc 为空 我如何创建位置 据我了解 我必须使用以下命令将位置包含在 build gradle 依赖项中 测试编译 但我找不到要包含的内容 你
  • 如何找到文件所在的挂载点?

    例如 我有一个具有以下路径的文件 media my mountpoint path to file txt 我已经有了完整的路径并且想要得到 media my mountpoint 我怎样才能做到这一点 最好使用 Python 并且不使用外
  • 带有参数化查询的 jsonb 存在运算符

    或者问号问题 我目前正在 php 中实现 postgres 数据库的搜索功能 该功能使用新的 jsonb 类型 为了实现这一目标 我正在执行带有命名占位符的准备好的语句 然而 我在尝试使用一些新的 postgres 时遇到了一个有趣的问题J
  • 自动将用户自定义字段添加到订单元数据中

    当客户下订单时 是否可以自动将客户自定义字段的值复制到订单的自定义字段 应该使用任何插件 扩展程序还是通过幕后自定义编码来完成 此自定义字段不需要显示在客户订单视图上 当我们通过 API 获取订单时 我们只需要它来区分订单是由 Consum
  • Java - notification() 与 notifyAll() - 可能死锁?

    有没有什么情况notify 可能会导致死锁 但是notifyAll 绝不 例如 在多个锁的情况下 notify 仅通知一个线程运行 该线程检查某个对象的锁定并再次等待 尽管另一个线程可以解锁该对象 如果使用notifyAll 所有线程都会被
  • Android:如何关闭 EditText 的 IME?

    如何关闭 IME 功能EditText 或者 如何避免显示 IME 键盘 我有一个布局 其中我的特殊键盘位于EditText所以不需要显示 IME 请理解 我无法将我的键盘实现为 IME 因为它是特定于此的EditText在任何其他情况下使
  • 更改itms-services安装提示信息

    现在 我有一个定义如下的链接 a itms services action download manifest url https loqi me install Geoloqi plist a 当用户单击此链接时 将显示以下消息 该消息由
  • C++ 2.5 字节(20 位)整数

    我知道这很荒谬 但我需要它来优化存储 有什么好的方法用C 实现吗 它必须足够灵活 以便我可以将它用作普通数据类型 例如Vector lt int20 gt 运算符重载等 如果存储是您主要关心的问题 我怀疑您需要相当多的 20 位变量 将它们
  • 如何使用 Jquery 编写“如果未单击”或“如果在元素外部单击”?

    我有点陷入如何阻止菜单执行 fadeOut 函数的问题 当我单击菜单上的主链接打开子菜单时 它就会淡出 目前代码如下 a main menu item click function if rtmenu visible rtmenu clic
  • 从字节数组创建 8 位图像

    字节数组是这样获得的 BufferedImage image new Robot createScreenCapture new Rectangle screenDimension byte array DataBufferByte get
  • 目录最后修改日期

    我想知道目录上次修改日期何时更改 我修改了特定目录中的文件 通过 FTP 但该目录的 LMD 没有更改 它应该如何运作 当添加 删除或重命名文件或子目录时 目录本身的 mtime 修改时间 会发生变化 修改目录中文件的内容不会更改目录本身
  • iphone:从相机捕获的图像改变方向

    我制作了一个 iPhone 应用程序来从相机捕获图像并在下一个视图中设置该图像 但问题是图像被旋转了 即风景图像变成肖像 肖像图像变成风景 我参考了很多代码但无法得到解决方案 我的代码是 void btnCapturePressed if