简体   繁体   English

UIImageView改变宽度和高度

[英]UIImageView change Width and Height

I've read various posts on here asking similar questions... I've tried various ways that were posted including bounds and frames etc. including the following: 我在这里阅读了各种帖子,询问类似的问题......我尝试了各种方式,包括边界和框架等,包括以下内容:

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

and: 和:

myImage.bounds = CGRectMake(0.0f, 0.0f,50.0f, 120.0f);

neither of those work. 这些都不是。

However, I find it interesting that the following code let's me move the Image around but doesn't change the width: 但是,我觉得有趣的是,以下代码让我移动图像但不改变宽度:

CGRect frameRect = myImage.frame;
frameRect.size.width = 50.0f;
frameRect.origin.x += 10.5f;
myImage.frame = frameRect;

So why don't any of these change the width/height of my ImageView? 那么为什么不改变我的ImageView的宽度/高度呢?

I found another post on here that basically states I have to right a small book of code to get it resize my image... is that true? 我在这里发现了另一篇文章,基本上说我必须修改一本小代码来调整我的图像大小...这是真的吗?

Such as this one: UIImage: Resize, then Crop 比如这一个: UIImage:调整大小,然后裁剪

certainly this is simpler than that?? 当然这比那简单?

The following will change the size of the UIImaveView, clipping the underlying image without resizing it and keeping it aligned to bottom left of view: 以下内容将更改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;

First off, you can't set the frame or bounds of the UIImage - that will only work on a UIImageView. 首先,你不能设置UIImage的框架或边界 - 这只适用于UIImageView。

I've found that changing the frame of a UIImageView causes the Image to be scaled to the new size. 我发现更改UIImageView的框架会导致Image 缩放到新的大小。 Sometimes, that's undesirable - and you want instead to crop the image. 有时,这是不合需要的 - 而你想要裁剪图像。

I can't tell if this is what you're asking for, but here's some code to crop an image to a specific size in a 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);

From what I get of the question, the OP wanted to change the size of the UIImageView when the size of the container UIView is changed. 从我得到的问题来看,OP想要在容器UIView的大小改变时改变UIImageView的大小。 The code below will do it... 下面的代码将做到......

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];

The key here are the foo.autoresizingMask = UIViewAutoresizingFlexibleWidth | 这里的关键是foo.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight and the bar.autoresizingMask = foo.autoresizingMask; UIViewAutoresizingFlexibleHeightbar.autoresizingMask = foo.autoresizingMask; lines. 线。 Forget either of these, and the whole jigmarole will stop working. 忘记其中任何一个,整个jigmarole将停止工作。

Well, if your read the documentation about UIIMage you can notice that is impossible to change any parameter of an UIImage after create it, the solution I've implemented for use high quality images for change some parameter of (for example) the SliderControl as Screw Image, is the next one: 好吧,如果您阅读有关UIIMage的文档,您可以注意到在创建它之后无法更改UIImage的任何参数,我实现的解决方案是使用高质量图像来更改某些参数(例如)SliderControl作为螺丝图片,是下一个:

UIImage *tumbImage= [UIImage imageNamed:@"screw.png"];
UIImage *screw = [UIImage imageWithData:UIImagePNGRepresentation(tumbImage) scale:2];

With that, I can to use 100x100 px image in my apps scaled to 50%. 有了这个,我可以在我的应用程序中使用100x100像素图像,缩放到50%。

Kind regards. 亲切的问候。

Try Using a UIScrollView. 尝试使用UIScrollView。 Add the UIImageView to the UIScrollView in Interface Builder you can then control the position and size of the image as follows: 将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];

If you tried those methods cannot work, the only way to do it is to add the constraint of width and height to the UIImageView. 如果您尝试这些方法无法工作,唯一的方法是将宽度和高度的约束添加到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];

Use myImageView.frame = myNewPosAndSize; 使用myImageView.frame = myNewPosAndSize; to resize or reposition your image view (as with any other view). 调整图像视图的大小或重新定位(与任何其他视图一样)。 It might confuse you that the image view draws its image with its original size, possibly exceeding its own dimensions. 您可能会混淆图像视图以其原始大小绘制其图像,可能超出其自身的尺寸。 To disable this use myImageView.clipsToBounds = NO; 要禁用此功能,请使用myImageView.clipsToBounds = NO;

You don't have to write a whole book, you can copy that code. 您不必编写整本书,也可以复制该代码。

I believe the UIImageView always draws the image at 0,0 at a 1.0 scale. 我相信UIImageView总是以1.0的比例绘制0,0的图像。 You'll need to resize the image if you want to continue using the UIImageView. 如果要继续使用UIImageView,则需要调整图像大小。

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

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