2011-09-21 71 views
1

我试图显示在肖像模式下通过相机拍摄的图像,但我总是得到它显示在我的UIImageView在Landascape模式。图像在被添加到UIImageView之前被缩放,但似乎这不是问题,因为我尝试了许多在网络上找到的不同解决方案(即使是一些相当聪明的解决方案,如来自Trevor's Bike Shed的解决方案)。UIImageView旋转我的UIImage

这里是我的代码:

UIImage *image = [UIImage imageWithContentsOfFile:imgPath]; 
CGRect newFrame = [scrollView frame]; 
UIImage *resizedImage = [ImageViewController imageFromImage:image scaledToSize:newFrame.size]; 
imageView = [[UIImageView alloc] initWithFrame:newFrame]; 
[imageView setImage:resizedImage]; 
[scrollView setContentSize:imageView.frame.size];  
[scrollView addSubview:imageView]; 
[imageView release]; 

imgPath是的图像来作为参数,并滚动视图是一个IBOutlet连接到一个UIScrollView的路径。

有什么我失踪的UIImageView? 正如我上面写的,似乎这个问题与缩放无关...

+0

我也遇到过这个问题。这个链接可能会帮助你。 http://stackoverflow.com/questions/1315251/how-to-rotate-a-uiimage-90-degrees –

回答

0

有一个UOmage的imageOrientation属性。检查出docs。它可以通过初始化设置:

UIImage *rotatedImage = [UIImage imageWithCGImage:[resizedImage CGImage] scale:1 orientation:UIImageOrientationUp] 

创建并返回与指定的规模和 取向因素的图像对象。

+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation 

参数:

  • imageRef石英图像对象。

  • scale解释图像数据时使用的比例因子。 指定比例因子1.0会导致图像的大小为 与图像的基于像素的尺寸相匹配。应用不同的 比例因子会更改图像的大小,如 (页面12)属性所报告的那样。

  • orientation图像数据的方向。您可以使用此参数 指定应用于图像的任何旋转因子。

  • 返回值指定Quartz图像的新图像对象,或 如果该方法无法从指定的 图像引用初始化图像,则为零。

0

你肯定,当你期望在resizedImageimageOrientation设置?不正确的缩放比例可能会使imageOrientation变得非常糟糕。在ImageView的或任何你想使用这里

+0

我一直在尝试的方法之一是指原始图像的imageOrientation,它似乎这是UIImageOrientationUp(0),这正是我所期望的。 –

+0

'UIImageOrientationUp'与原始图像相同吗?尽量不要缩放;只需将原始图像放在视图中即可。它工作正常吗?在桌面上的预览中打开原始图像,并确保它是正确的。将缩放的图像保存到磁盘(模拟器)并在预览中打开它。看哪个部件有问题。 –

+0

2张图片的方向相同。我正在从相机拍摄图像,如何在模拟器上测试程序? –

0
UIImage* image=[UIImage imageNamed:@"abc.jpg"]; 
UIImageOrientation orientation=image.imageOrientation; 

使用图像,

UIImageView* imageView=[[UIImageView alloc] initWithImage:image]; 

后的图像重置方向是使用前

UIImage* image1=[UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:orientation]; 

image1的是在你的形象定位之前是