簡體   English   中英

UIImageView改變寬度和高度

[英]UIImageView change Width and Height

我在這里閱讀了各種帖子,詢問類似的問題......我嘗試了各種方式,包括邊界和框架等,包括以下內容:

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

和:

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

這些都不是。

但是,我覺得有趣的是,以下代碼讓我移動圖像但不改變寬度:

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

那么為什么不改變我的ImageView的寬度/高度呢?

我在這里發現了另一篇文章,基本上說我必須修改一本小代碼來調整我的圖像大小...這是真的嗎?

比如這一個: UIImage:調整大小,然后裁剪

當然這比那簡單?

以下內容將更改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的框架或邊界 - 這只適用於UIImageView。

我發現更改UIImageView的框架會導致Image 縮放到新的大小。 有時,這是不合需要的 - 而你想要裁剪圖像。

我不知道這是否是你要求的,但這里有一些代碼可以在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 = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeightbar.autoresizingMask = foo.autoresizingMask; 線。 忘記其中任何一個,整個jigmarole將停止工作。

好吧,如果您閱讀有關UIIMage的文檔,您可以注意到在創建它之后無法更改UIImage的任何參數,我實現的解決方案是使用高質量圖像來更改某些參數(例如)SliderControl作為螺絲圖片,是下一個:

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

有了這個,我可以在我的應用程序中使用100x100像素圖像,縮放到50%。

親切的問候。

嘗試使用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];

如果您嘗試這些方法無法工作,唯一的方法是將寬度和高度的約束添加到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];

使用myImageView.frame = myNewPosAndSize; 調整圖像視圖的大小或重新定位(與任何其他視圖一樣)。 您可能會混淆圖像視圖以其原始大小繪制其圖像,可能超出其自身的尺寸。 要禁用此功能,請使用myImageView.clipsToBounds = NO;

您不必編寫整本書,也可以復制該代碼。

我相信UIImageView總是以1.0的比例繪制0,0的圖像。 如果要繼續使用UIImageView,則需要調整圖像大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM