简体   繁体   English

uiview内的uiview也位于按钮内,显示uiimage模糊

[英]uiimageview within a uiview also within a button shows uiimage blurred

So I have a button with a custom uiview that contain a uiimageview. 所以我有一个带有自定义uiview的按钮,其中包含uiimageview。

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];

My image is blurry no matter how much in increase or decrease the size. 无论增加或减小大小,我的图像都是模糊的。 What am i doing wrong here? 我在这里做错了什么?

Try setting a content mode ie: 尝试设置内容模式,即:

myImageView.contentMode = UIViewContentModeScaleAspectFit;

Read more about content modes: https://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/c/tdef/UIViewContentMode 阅读有关内容模式的更多信息: 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;

The other folks have mentioned two of the possible causes of fuzziness 其他人提到了两个模糊的可能原因

  • the image size doesn't match the frame size, resulting in scaling 图像尺寸与帧尺寸不匹配,导致缩放
  • the image aspect ratio is being adjusted due to the content mode 由于内容模式,正在调整图像宽高比

The third possible cause is the use of odd numbers in the frame. 第三个可能的原因是在帧中使用奇数。 From my experience, if the center.x and/or center.y of a UIView is not a whole number, then fuzziness results. 根据我的经验,如果UIView的center.x和/或center.y不是整数,则会导致模糊。 For example, if you set the frame as 例如,如果您将框架设置为

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

then things get fuzzy because the center point of said view will be CGPoint( 114.5, 114.5 ) . 然后事情就变得模糊了,因为所说视图的中心点是CGPoint( 114.5, 114.5 ) This problem is mainly seen on the non-retina devices. 此问题主要出现在非视网膜设备上。 Retina devices have two pixels per point, so half-a-point coordinates are ok, but other off-grid coordinates could still cause fuzziness. 视网膜设备每点有两个像素,因此半点坐标是可以的,但是其他离网坐标仍然可能导致模糊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM