繁体   English   中英

粉碎initWithFrame图像大小的高度和宽度

[英]crush the height and width of the initWithFrame image size

我的UIImageView有一个小问题,我尝试动态放置图像,所以我从这样开始

self.picture_view = UIImageView.alloc.initWithFrame (CGRectMake (0, 16,45, 46))

但我想计算通过他的每张图像的帧大小。

所以我喜欢这样。

 picture_frame = picture_view.frame;
 picture_frame.size = picture_view.size;
 picture_view.frame = picture_frame;

但是当我这样做时

NSLog (picture_frame.size.inspect)

每张图像给我45、46,那么如何为我恢复显示正确尺寸的图像尺寸和帧替代器

先感谢您。

PS:我做得很好picture_view.image = UIImage.imageNamed(my_picture)

您实际上是在将ImageViews框架设置为其本身的框架。 您需要根据实际的UIImage进行操作。 您可以通过使用图像初始化视图来自动执行此操作。

防爆。

UIImageView *pictureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whatever.png"]];
//The size is already set correctly, but if you want to change the x or y origin do like so

pictureView.frame = CGRectMake(0, 16, pictureView.frame.size.width, pictureView.frame.size.height);

编辑:这个答案是用Objective-C编写的,但是核心思想应该很容易转化为rubymotion。


如果尝试创建UIImageView ,然后调整其大小以适合分配给它的UIImage ,则应尝试以下操作:

使用图像创建一个imageView:

UIImage *image = [UIImage imageNamed:@"myPicture.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

更改图像,并调整imageView的大小以适合:

UIImage *anotherImage = [UIImage imageNamed:@"anotherPicture.png"];
imageView.image = anotherImage;

CGRect imageViewFrame = imageView.frame;
imageViewFrame.size = anotherImage.size;
imageView.frame = imageViewFrame;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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