简体   繁体   中英

Set UIImageView Size?

i have following problem: i generate subviews UIView in an UIScrollView including UIImageViews.

NSArray *bilder = [[NSArray alloc] initWithObjects:@"lol.jpg", @"lol2.jpg", nil];
NSArray *added = [[NSMutableArray alloc] init];
UIImageView *tmpView;

for (NSString *name in bilder) {
    UIImage *image = [UIImage imageNamed:name];
    tmpView = [[UIImageView alloc] initWithImage:image];
    tmpView.contentMode = UIViewContentModeScaleAspectFit;
    tmpView.frame = CGRectMake(0, 0, 300, 300);

    [added addObject:tmpView];

    [tmpView release];
}

CGSize framy = mainFrame.frame.size;

NSUInteger x = 0;

for (UIView *view in added) {
    [mainFrame addSubview:view];
    view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
}

mainFrame.scrollEnabled = YES;
mainFrame.pagingEnabled = YES;
mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);
mainFrame.backgroundColor = [UIColor blackColor];

i get image file names out of an array. now i want to resize the imageview to a specific size, but it won't work. any advice?

thanks + regards

Change tmpView.frame to use a new CGRect:

tmpView.frame = CGRectMake(tmpView.frame.origin.x,
                           tmpView.frame.origin.y,
                           newWidth,
                           newHeight);

UIImage缺少NSImagesetSize:方法,但这篇文章似乎在UIImage上添加了一个使用类别的scaleToSize:方法。

What I see is:

view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);

But if you look at CGRectMake...

Declaration: CGRect CGRectMake (
   CGFloat x,
   CGFloat y,
   CGFloat width,
   CGFloat height
);

You are setting the X value, not the Y value. I expect, based on your later call to:

mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);

that you intend to make the these images appear on top of each other, not side by side.

Suggest:

view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);

to

view.frame = CGRectMake(0,framy.height * x++, framy.height, framy.width);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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