2013-06-19 157 views
0

我希望在每次旋轉時更改視圖的背景圖像。圖像會發生變化,但是當旋轉是水平時,會將其切斷,就好像它是一幅肖像圖像。這就是我的意思:enter image description here旋轉對背景圖像的影響

這個方向是水平的,但圖像被裁剪爲肖像尺寸。 actual image適合水平放置。

這裏是我的代碼:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
// called after rotation occurs 

if (fromInterfaceOrientation == UIInterfaceOrientationPortrait) { 
    // now horizontal 
    imageToLoad = [UIImage imageNamed:@"H0.png"]; 
} else { 
    // now portrait 
    imageToLoad = [UIImage imageNamed:@"P0.png"]; 
} 
UIGraphicsBeginImageContext(self.view.frame.size); 
[imageToLoad drawInRect:self.view.bounds]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

self.view.backgroundColor = [UIColor colorWithPatternImage:image]; 
} 

我怎麼能解決這個問題? 謝謝。

+0

代碼看起來很好,除非你在這之前和之後做了一些奇怪的事情。 :) 這可能是由於緩存圖像可能是你以前有錯誤的名字嗎? 嘗試清理構建目錄,看看它是否有幫助。 – nsuinteger

回答

0

爲什麼使用背景顏色?添加一個UIImageView與適當的自動調整掩碼和內容模式。

+0

之前我爲此使用過一個圖像視圖,並且在旋轉時調整大小非常困難。 – eulr