CGImageRef转换为UIImage会旋转我的照片 - 在iOS 4模拟器或设备上进行调试

4

我有一个简单代码的问题。无论是从相册选择还是使用相机拍摄的照片都会被旋转。起初我以为是UIView中的设置问题,但当我使用CGImageRef将传入的UIImage复制到另一个UIImage时,它仍然发生了旋转。我这样做是因为这是确保我使用副本最简单的方式。如果我搞错了,请纠正这段代码。

代码如下:

- (id)initWithImage:(UIImage *)image {
    if ((self = [super init]) && (image != nil)) {
        CGImageRef tmpImageRef = [image CGImage];
        puzzle = [[UIImage alloc] initWithCGImage:tmpImageRef];
    }

    return self;
}

调试器:
    This GDB was configured as "--host=i386-apple-darwin --target=arm-apple-darwin".tty /dev/ttys001
Loading program into debugger…
sharedlibrary apply-load-rules all
Program loaded.
target remote-mobile /tmp/.XcodeGDBRemote-25694-49
Switching to remote-macosx protocol
mem 0x1000 0x3fffffff cache
mem 0x40000000 0xffffffff none
mem 0x00000000 0x0fff none
run
Running…
[Switching to thread 11523]
[Switching to thread 11523]
Re-enabling shared library breakpoint 1
Re-enabling shared library breakpoint 2
continue
2010-07-13 15:09:17.159 Golovomka[693:307] Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.
2010-07-13 15:09:17.172 Golovomka[693:307] Using two-stage rotation animation is not supported when rotating more than one view controller or view controllers not the window delegate
Current language:  auto; currently objective-c
(gdb) print (CGSize)[image size]
$1 = {
  width = 1536, 
  height = 2048
}
(gdb) n
28                  puzzle = [[UIImage alloc] initWithCGImage:tmpImageRef];
(gdb)
31          return self;
(gdb) print (CGSize)[puzzle size]
$2 = {
  width = 2048,
  height = 1536
}
(gdb)

第一次打印出现在CGImageRef实例化行。非常感谢你的帮助。正如我所说,这种情况不会在模拟器中发生,只有当我将代码部署到真实设备时才会发生。

请注意,这篇文章曾经说过这个问题只会在设备上调试时出现,而不会在模拟器中出现。但是我已经将我的iphone 3gs相机拍摄的照片复制到了模拟器中,完全相同的问题也发生了。因此,如果你手头有一张2048x1536的图片,你应该能够在模拟器中复制这个问题。
1个回答

2

UIImage有一个方向属性,你提取UIImage中的CGImage时忽略了它。你应该这样做:

if (self = [super init]) {
    puzzle = image; 
}

这确实保留了方向。我刚刚发现了这篇文章https://dev59.com/YnM_5IYBdhLWcg3wt1k0,涵盖了类似的内容。之前我用这种方式时代码崩溃了,我以为是因为图像稍后被释放了。现在看来不是这样,所以我一定搞砸了其他东西,当我搜索解决方案时,我最终使用了imageref方法;我认为这对于现代语言来说有点糟糕。谢谢! - Diziet
作为进一步的说明,在开发的后期,我最终进行了适当的翻译和旋转图像,以消除在不同库和系统中处理方向数据时出现的问题,例如核心图形、OpenCV和位操作。 - Diziet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接