0

一連の画像をダウンロードし、それらを水平方向にスクロール可能な に並べて表示していますUiScrollView

Android ではLinearLayoutScrollView適切な重力が設定されています。画像がダウンロードされると、それらはレイアウトに追加され、適切に表示されます - 正しい比率で、親ビューを引き伸ばしてそれらに対応します。

iOSでは、私は苦労しています。私はAsyncImageViewを使用して画像をロードしていますが、これは正常に機能します。問題は、ビューがどうなるかを知る前に、各ビューをその境界で初期化する必要があることです。

for(NSString* url in self.imageURLs){
    AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:self.imageScroller.frame];
    [iView showActivityIndicator];
    [iView setImageURL:[NSURL URLWithString:url]];
    [self.imageScroller addSubview:iView];
}

これは機能しません。すべての画像が互いに重ねて読み込まれ、フレームに合わせて不均衡に引き伸ばされます。

iOS で「重力」または「フロート」のようなレイアウトを作成する簡単な方法はありますか? そうでない場合、どのように画像をレイアウトすればよいですか?

編集

明確にするために: これらの画像は縦横比が異なるため、読み込まれるまでサイズを知ることはできません。

これが私のAndroidレイアウトです

ここに画像の説明を入力

4

4 に答える 4

0

最終的に、データを ajax ペイロードに追加することで、これを正しく行うのが最も簡単になりました。

CGRect currentFrame = self.imageScroller.bounds;
CGFloat currentX = self.imageScroller.bounds.origin.x;
for(NSDictionary* snap in self.imageURLs){
    NSNumber *iWidth = [snap objectForKey:@"width"];
    NSNumber *iHeight = [snap objectForKey:@"height"];
    CGFloat width = [iWidth floatValue] * currentFrame.size.height / [iHeight floatValue];
    CGRect bounds = CGRectMake(currentX, currentFrame.origin.y, width, currentFrame.size.height);
    AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:bounds];
    [iView showActivityIndicator];
    [iView setImageURL:[NSURL URLWithString:[snap objectForKey:@"url"]]];
    [self.imageScroller addSubview:iView];
    currentX += width;
}
self.imageScroller.contentSize = CGSizeMake(currentX,currentFrame.origin.y);
于 2013-08-06T14:20:38.493 に答える
0

これを試して:

CGRect currentFrame = self.imageScroller.bounds;
for(NSString* url in self.imageURLs){
    AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:currentFrame];
    [iView showActivityIndicator];
    [iView setImageURL:[NSURL URLWithString:url]];
    [self.imageScroller addSubview:iView];
    currentFrame.origin.x += current.frame.size.width; // that is if you put them next to each other horizontally, otherwise use origin.y and size.height
}

また、imageScroller の contentSize をまだ設定していない場合は忘れずに設定してください。

于 2013-08-06T12:23:13.100 に答える
0

これを行う最も簡単な方法は、各画像のフレームを計算して設定することです。

int i = 0;

float width = self.imageScroller.frame.size.width;
float height = self.imageScroller.frame.size.height;
float y = 0;

for(NSString* url in self.imageURLs){
    ++i;

    float x = i * width;

    AsyncImageView *iView = [[AsyncImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [iView showActivityIndicator];
    [iView setImageURL:[NSURL URLWithString:url]];
    [self.imageScroller addSubview:iView];
}

ただし、大量の画像をロードする場合、これには問題があります。

これを行う最善の方法は、UIPageViewController を使用することです。ここのような場所を参照してください... http://www.raywenderlich.com/forums/viewtopic.php?f=5&t=3120

于 2013-08-06T12:23:19.637 に答える