2011-09-21 138 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的是在你的形象定位之前是