2013-04-29 209 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]; 

和;)