2014-03-31 40 views
0

所以我有一个自定义uiview包含uiimageview的按钮。在uiview中的uiimageview也在按钮内显示uiimage模糊

UIButton *myButton = [[UIButton alloc] init]; 
myButton.frame = CGRectMake(0, 0, 29, 29); 

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 29, 29)]; 


UIImage *myImage = [UIImage imageNamed:@"sexy-pic-of-me"]; 
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; 
myImageView.frame = CGRectMake(0,0,29,29); 

[myView addSubview:myImageView]; 
[myButton addSubview:myView]; 

[self.view addSubview: myButton]; 

无论增大或减小大小,我的图像都很模糊。我在这里做错了什么?

+0

可能是因为您的图片尺寸不是1:1的比例,并且因为尺寸较大或很小。 – Roecrew

+0

你的图像的原始尺寸是什么? – Stonz2

+0

图片尺寸为29X29 – marshy101

回答

0

的其他人都提到两个模糊的可能原因

  • 图像大小与帧大小不匹配,导致缩放
  • 图像纵横比被由于内容模式

第三可能的原因是在该帧中使用奇数的调整。根据我的经验,如果UIView的center.x和/或center.y不是一个整数,则会产生模糊性。例如,如果你设置的帧

someView.frame = CGRect(100, 100, 29, 29); 

后来的事情,因为中心点说视图将CGPoint(114.5, 114.5)得到模糊。这个问题主要出现在非视网膜设备上。视网膜设备每点有两个像素,所以半点坐标可以,但其他离网坐标仍然会导致模糊。

1

尝试设置内容模式,即:

myImageView.contentMode = UIViewContentModeScaleAspectFit; 

了解更多有关内容模式: https://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/c/tdef/UIViewContentMode

UIViewContentMode 
Options to specify how a view adjusts its content when its size changes. 

typedef enum { 
    UIViewContentModeScaleToFill, 
    UIViewContentModeScaleAspectFit, 
    UIViewContentModeScaleAspectFill, 
    UIViewContentModeRedraw, 
    UIViewContentModeCenter, 
    UIViewContentModeTop, 
    UIViewContentModeBottom, 
    UIViewContentModeLeft, 
    UIViewContentModeRight, 
    UIViewContentModeTopLeft, 
    UIViewContentModeTopRight, 
    UIViewContentModeBottomLeft, 
    UIViewContentModeBottomRight, 
} UIViewContentMode;