0

私はUIViewをサブクラス化し、リモート画像を取得してから、drawRectを使用してビューに描画しています。これは正常に機能しますが、ビュー フレームに設定した内容に基づいて画像が歪んでしまいます。

UIViewContentModeScaleAspectFill の contentMode を UIView に追加できますが、描画している UIImage にはまったく適用されないようです。ビューのフレーム サイズに従うため、UIImage を押しつぶします。

次のようなことをする必要があるようですが、 contentMode 設定に従うのではなく、フレーム内で画像を 100% ズームするだけです。

[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

イメージを contentMode プロパティに従うようにするには、drawRect で何か特定の操作を行う必要がありますか?

4

2 に答える 2

2

最も簡単な解決策は、UIViewの代わりにUIImageViewをサブクラス化し、クラスのcontentModeプロパティを使用することだと思います。次に、drawRectを呼び出す必要はなく、画像をimageViewにロードするだけです。

または、UIViewをサブクラス化する必要がある場合は、UIImageViewをビューに追加し、スプリングとストラットを設定し、contentModeを設定して、画像をロードします。

于 2011-08-19T20:18:38.737 に答える
0

あなたはこのようなものを使うことができます

    // If we have a custom background image, then draw it, othwerwise call super and draw the standard nav bar
- (void)drawRect:(CGRect)rect
{
  if (navigationBarBackgroundImage)
    [navigationBarBackgroundImage.image drawInRect:rect];
  else
    [super drawRect:rect];
}

// Save the background image and call setNeedsDisplay to force a redraw
-(void) setBackgroundWith:(UIImage*)backgroundImage
{
  self.navigationBarBackgroundImage = [[[UIImageView alloc] initWithFrame:self.frame] autorelease];
  navigationBarBackgroundImage.image = backgroundImage;
  [self setNeedsDisplay];
}
于 2012-05-15T12:20:19.977 に答える