5

UIImageView には本当に奇妙な問題があります。ビューに追加する 45x45 ピクセルの画像 (RGB png) があります。ビューに追加した後、画像がぼやけていることがわかります。シミュレーター (左) と Xcode (右) の同じ画像を次に示します。

代替テキスト
(ソース: partywithvika.com )

代替テキスト
(ソース: partywithvika.com )

このinitWithImageコードを持つカスタムUIImageViewクラスがあります:

- (id) initWithImage:(UIImage*) image {
    self = [super initWithImage:image];

    self.frame = CGRectMake(0, 0, 45, 45);
    self.contentMode = UIViewContentModeScaleAspectFit;

    self.quantity = 1;
    if (self) {
        self.label = [[UITextField alloc]initWithFrame:CGRectMake(0,40,45,25)];
        self.label.font = [UIFont systemFontOfSize:16];
        self.label.borderStyle = UITextBorderStyleNone;
        self.label.enabled = TRUE;
        self.label.userInteractionEnabled = TRUE;
        self.label.delegate = self;
        self.label.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        self.label.textAlignment = UITextAlignmentCenter;
    }
    self.userInteractionEnabled = TRUE;

    // Prepare 3 buttons: count up, count down, and delete
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.deleteButton.hidden = NO;
    self.deleteButton.userInteractionEnabled = YES;
    self.deleteButton.titleLabel.font  = [UIFont systemFontOfSize:20];
    self.deleteButton.titleLabel.textColor = [UIColor redColor];
    [self.deleteButton setTitle:@"X" forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(deleteIcon:) forControlEvents:UIControlEventTouchUpInside];

    self.upCountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.upCountButton.hidden = NO;
    self.upCountButton.userInteractionEnabled = YES;
    [self.upCountButton setTitle:@"+" forState:UIControlStateNormal];
    [self.upCountButton addTarget:self action:@selector(addQuantity:) forControlEvents:UIControlEventTouchUpInside];

    self.downCountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.downCountButton.hidden = YES;
    self.downCountButton.userInteractionEnabled = YES;
    [self.downCountButton setTitle:@"-" forState:UIControlStateNormal];
    [self.downCountButton addTarget:self action:@selector(removeQuantity:) forControlEvents:UIControlEventTouchUpInside];
    return self;
}

私は次のように作成します:

UIImage *desertIcon = [UIImage imageNamed:@"desert.png"];
IconObj *desertIconView = [[IconObj alloc] initWithImage:desertIcon];
desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.type = [IconObj TYPE_DESERT];
[self.view addSubview:desertIconView];
[desertIconView release];

表示された画像が、ファイルに保存された画像よりもそうであるのはなぜですか?

4

7 に答える 7

8

一般に、UIKitを使用してiPhoneで行うことはすべて、ピクセル単位で調整する必要があります。ピクセルの配置に関する問題は、通常、ぼやけとして現れます(これは特にテキストや画像に当てはまります)。これが、ぼやけたビューを見つけたときに、最初にcenterプロパティを設定しているかどうかを確認する理由です。を設定するcenterと、フレームのx、y、高さ、幅がその中心点を中心に調整されます...多くの場合、小数値になります。

CGRectIntegral次のように、フレームで使用してみてください。

desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.frame = CGRectIntegral(desertIconView.frame);

これは機能する可能性がありますが、機能しない場合は、プロパティをframe使用せずに手動で設定して、小数値がないことを確認してください。center

編集:おっと!OPが彼自身の質問に答えたことに気づかなかった...とにかく情報提供の理由でこれを残しておきます。

于 2010-03-22T00:25:45.760 に答える
3

私はこの問題を抱えていて、それが私を狂わせていました。調査の結果、画像がフレームよりも小さいことが判明したため、拡大すると画像がぼやけました。高解像度の画像を入れると、問題はなくなりました。

画像サイズが UIImageView フレーム サイズよりも大きいことを確認してください。

于 2010-03-13T12:44:04.140 に答える
3

IconObj の幅は 45 ピクセルです。IconObj の中心を 265 に移動すると、フレームが (242.5, VERTICAL_POINT_ICON - 25*0.5, 45, 25) になります。フレームパラメータの一部が整数でない場合、画像は常にぼやけます。

解決策、フレーム パラメータを自分で計算します (中心は使用しないでください)。そして、常に整数にします (NSInteger にキャストし、ceil、floor、または round を使用します)。

desertIconView.frame = CGRectMake((NSInteger)(265 - 45*0.5),
                                  (NSInteger)(VERTICAL_POINT_ICON - 25*0.5),
                                  45, 25);
于 2010-04-12T05:58:12.700 に答える
1

私がやったことは、もちろん、より大きな画像を45x45 UIImageViewにロードすることです

contentMode = UIViewContentModeScaleAspectFit;
于 2009-08-10T14:00:27.307 に答える
1

私はちょうど同じ問題を抱えていました。最初に思ったのですが、メガネは必要ですか?

次に、奇数を指定すると、画像 (またはラベル) を中央に配置することは不可能であることに気付きました。画像を中央に配置してシャープに保ちたい場合は、画像のサイズを 46*46 などに変更する必要があります。

または、45*45 のままにしておくこともできますが、画像の中心を右に 1 ピクセルにするか左に 1 ピクセルにするかを決める必要があります。(またはぼやけたままにします)。

于 2011-10-31T13:25:38.587 に答える
0

Try to align your IconObj view at screen pixels. If it's really 45x45 then you should set the center to something like CGPointMake(x + 0.5f, y + 0.5f).

You should also double check image size in pixels (e.g in Preview.app Command-I).

于 2009-08-09T09:46:42.810 に答える
0

オフセットの問題の完全なソース:

  contentImage  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

  UIImage *cImage = [UIImage imageNamed:image];
  self.contentImage.image = cImage;

  [self.contentImage setFrame:CGRectMake(0, 0, cImage.size.width, cImage.size.height)];

  CGFloat offset = 0.0f;

  if ( ( (int)cImage.size.width % 2 ) && ( (int)cImage.size.height % 2 ) ) {
    offset = 0.5f;
  }

  [self.contentImage setCenter:CGPointMake(256.0f + offset, 256.0f + offset)];
于 2012-01-13T12:19:43.210 に答える