【问题标题】:uiimageview within a uiview also within a button shows uiimage blurreduiview 内的 uiimageview 也在按钮内显示 uiimage 模糊
【发布时间】:2014-05-11 09:49:00
【问题描述】:

所以我有一个带有自定义 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];

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

【问题讨论】:

  • 可能是因为您的图像尺寸不是 1:1 的比例,而是因为它太大或太小。
  • 图片的原始尺寸是多少?
  • 图片尺寸为29X29

标签: ios iphone objective-c uiview uiimageview


【解决方案1】:

其他人提到了两个可能导致模糊的原因

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

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

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

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

【讨论】:

    【解决方案2】:

    尝试设置内容模式,即:

    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;
    

    【讨论】:

      猜你喜欢
      • 2016-09-29
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      相关资源
      最近更新 更多