设置 UIImageView 大小?

发布于 2024-08-30 09:44:48 字数 1003 浏览 6 评论 0原文

我有以下问题:我在 UIScrollView 中生成子视图 UIView,包括 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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2024-09-06 09:44:48

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

UIImage lacks the setSize: method of NSImage, but this post seems to add a scaleToSize: method using a category on UIImage.

赠我空喜 2024-09-06 09:44:48

更改 tmpView.frame 以使用新的 CGRect:

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

Change tmpView.frame to use a new CGRect:

tmpView.frame = CGRectMake(tmpView.frame.origin.x,
                           tmpView.frame.origin.y,
                           newWidth,
                           newHeight);
请远离我 2024-09-06 09:44:48

我看到的是:

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

但是如果你看看 CGRectMake...

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

你正在设置 X 值,而不是 Y 值。我希望,根据您稍后的调用:

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

您打算使这些图像显示在彼此之上,而不是并排显示。

建议:

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文