Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIImageView更改宽度和高度_Iphone - Fatal编程技术网

Iphone UIImageView更改宽度和高度

Iphone UIImageView更改宽度和高度,iphone,Iphone,我读过很多关于这里的帖子,问过类似的问题。。。我尝试了各种发布方式,包括边界和框架等,包括以下内容: myImage.frame = CGRectMake(0.0f, 0.0f,50.0f, 50.0f); 以及: 这两个都不管用 但是,我发现有趣的是,下面的代码让我移动图像,但不改变宽度: CGRect frameRect = myImage.frame; frameRect.size.width = 50.0f; frameRect.origin.x += 10.5f; myImage.f

我读过很多关于这里的帖子,问过类似的问题。。。我尝试了各种发布方式,包括边界和框架等,包括以下内容:

myImage.frame = CGRectMake(0.0f, 0.0f,50.0f, 50.0f);
以及:

这两个都不管用

但是,我发现有趣的是,下面的代码让我移动图像,但不改变宽度:

CGRect frameRect = myImage.frame;
frameRect.size.width = 50.0f;
frameRect.origin.x += 10.5f;
myImage.frame = frameRect;
那么,为什么这些都不改变ImageView的宽度/高度呢

我在这里找到了另一篇文章,基本上是说我必须修改一本小的代码书,才能让它调整我的图像大小。。。这是真的吗

比如这个:


当然,这比那更简单???

你不必写一整本书,你可以复制代码


我相信UIImageView总是以1.0的比例以0,0绘制图像。如果要继续使用UIImageView,则需要调整图像大小。

使用
myImageView.frame=myNewPosAndSize
调整图像视图的大小或重新定位图像视图(与任何其他视图一样)。图像视图以其原始大小绘制图像可能会使您感到困惑,可能会超出其自身的尺寸。要禁用此功能,请使用myImageView.clipsToBounds=NO

首先,您不能设置UIImage的帧或边界-这仅适用于UIImageView

// Create the constraint in code
NSLayoutConstraint *constraint0 = [NSLayoutConstraint constraintWithItem: myImage attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant: yourNewsWidth];


NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem: myImage attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant: yourNewsHeight];

[myImage addConstraint:constraint0];
[myImage addConstraint:constraint1];
我发现更改UIImageView的帧会导致图像缩放到新的大小。有时,这是不受欢迎的,而您希望裁剪图像

我不知道这是否是您所要求的,但这里有一些代码可以在UIImageView中将图像裁剪为特定大小:

UIImage *myImage = [UIImage imageNamed:@"photo.png"];

CGRect cropRect = CGRectMake(0.0, 0.0, 320.0, 44.0));
CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);

UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 

CGImageRelease(croppedImage);

从我对这个问题的理解来看,OP希望在更改容器UIView的大小时更改UIImageView的大小。下面的代码可以做到这一点

UIView * foo = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)] autorelease];
foo.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIImageView * bar = [[UIImageView alloc] initWithImage:
                     [UIImage imageNamed:@"test.png"]];
bar.autoresizingMask = foo.autoresizingMask;
[foo addSubview:bar];
[self.view addSubview:foo];

这里的键是foo.autoresizingMask=uiviewsautoresizingflexiblewidth | uiviewsautoresizingflexiblewhight和bar.autoresizingMask=foo.autoresizingMask;线。忘记这两个选项,整个jigmarole将停止工作。

尝试使用UIScrollView。将UIImageView添加到Interface Builder中的UIScrollView,然后可以按如下方式控制图像的位置和大小:

    CGRect rect = [scrollView frame];

    rect.origin.x = 50.0f;
    rect.origin.y = 0.0f;
    rect.size.width = 320.0f;
    rect.size.height = 150.0f;

    [scrollView setFrame:rect];

以下操作将更改UIImaveView的大小,剪裁基础图像而不调整其大小,并使其与视图的左下角对齐:

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

imageView.contentMode = UIViewContentModeBottomLeft; // This determines position of image
imageView.clipsToBounds = YES;

好的,如果您阅读了有关UIIMage的文档,您会注意到在创建UIIMage后不可能更改UIIMage的任何参数,我为使用高质量图像更改某些参数(例如,将滑块控件更改为螺旋图像)而实施的解决方案是下一个:

UIImage *tumbImage= [UIImage imageNamed:@"screw.png"];
UIImage *screw = [UIImage imageWithData:UIImagePNGRepresentation(tumbImage) scale:2];
有了它,我可以在我的应用程序中使用100x100像素的图像,比例可以达到50%


亲切的问候

如果您尝试了这些方法,但这些方法不起作用,那么唯一的方法就是向UIImageView添加宽度和高度约束

// Create the constraint in code
NSLayoutConstraint *constraint0 = [NSLayoutConstraint constraintWithItem: myImage attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant: yourNewsWidth];


NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem: myImage attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant: yourNewsHeight];

[myImage addConstraint:constraint0];
[myImage addConstraint:constraint1];

我明白了,我的问题不是以最好的方式提出的,我应该说缩放图像是可以的。我可以选择“按比例填充”的模式,我上面的代码可以很好地工作。谢谢你的帮助。我讨厌别人用foo和bar