2013-04-29 123 views
2

我正在嘗試將UIImage添加到UIView。 圖像正好在視圖的大小 - 當我定義視圖矩形。 不知何故,我看到圖像顯示得更寬,更高(延伸)。 爲什麼我的觀點是改變圖像尺寸?UIView更改圖像的大小?

UIImage *strip=[UIImage imageNamed:@"albumStrip.png"]; 
    UIImageView *imageView=[[UIImageView alloc]initWithImage:strip]; 
    UIView * aView = [ [UIView alloc] initWithFrame:CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10) ]; 
    aView.backgroundColor = [UIColor whiteColor]; 
    aView.tag = 31000; 
    aView.layer.cornerRadius=1; 
    [aView addSubview:imageView]; 

編輯: 我可以看到我的形象是640×960。是否有可能爲iPhone4 UIImage不知道如何把它和因子2分開?

回答

2

使用

imageView.layer.masksToBounds = YES; 將限制圖像爲寬的和高

如果masksToBounds屬性被設置爲YES,即延長其邊界外的層中的任何的子層將被剪切到這些邊界。在這種情況下,將層看作是其子層上的窗口;窗口邊緣以外的任何東西都將不可見。當masksToBounds爲NO時,不會發生裁剪,並且任何延伸到圖層邊界之外的子圖層都將全部可見(只要它們不在任何啓用了遮罩的超級圖層的邊緣之外)。

+0

感謝它沒有工作:( – Curnelious 2013-04-29 07:57:05

+0

獲取錯誤或什麼隊友? – 2013-04-29 07:58:06

+1

嘗試'aview.layer.masksToBounds = YES;' – 2013-04-29 07:59:04

1

當然,您的圖片將被拉伸,無法顯示在您的視圖中。 因爲它的框架比UIView的尺寸大得多。 你應該設置的UIImageView的框架是大小爲你的UIView一樣,將其添加爲子視圖:當然,如果你能做出的UIImageView的尺寸更小的

UIImage *strip=[UIImage imageNamed:@"albumStrip.png"]; 
CGRect frame = CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10); 
// create the uiimageView with the same frame size as its parentView. 
UIImageView *imageView=[[UIImageView alloc] initWithFrame:frame]; 
// set the image 
imageView.image = strip; 
// create the view with the same frame 
UIView * aView = [ [UIView alloc] initWithFrame:frame]; 
aView.backgroundColor = [UIColor whiteColor]; 
aView.tag = 31000; 
aView.layer.cornerRadius=1; 
[aView addSubview:imageView]; 

和;)