如何缩放UIButton的imageView?
我使用[UIButton setImage:forState:]
创build了一个名为“button”的UIButton实例。 button.frame大于图像的大小。
现在我想缩小这个button的图像。 我试着改变button.imageView.frame
, button.imageView.bounds
和button.imageView.contentMode
,但都似乎无效。
任何人都可以帮助我缩放一个UIButton
的imageView?
我创build了这样的UIButton
:
UIButton *button = [[UIButton alloc] init]; [button setImage:image forState:UIControlStateNormal];
我试图像这样缩放图像:
button.imageView.contentMode = UIViewContentModeScaleAspectFit; button.imageView.bounds = CGRectMake(0, 0, 70, 70);
和这个:
button.imageView.contentMode = UIViewContentModeScaleAspectFit; button.imageView.frame = CGRectMake(0, 0, 70, 70);
对于原来的海报,这里是我find的解决scheme:
commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
这将允许您的button水平缩放。 还有一个垂直的设置。
花了我几个小时来弄清楚这个(财产的命名是非常不直观的),所以我想分享。
我遇到类似的问题,我有一个自定义button的背景图像(一个带边框)和一个图像(标志)。 我希望国旗被缩小并居中。 我试图改变imageView的属性,但没有成功,看到这个图像 –
在我的实验中,我尝试了:
button.imageEdgeInsets = UIEdgeInsetsMake(kTop,kLeft,kBottom,kRight)
我达到了预期的结果:
使用
button.contentMode = UIViewContentModeScaleToFill;
不
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
更新:
正如@Chris所评论的,
为了使这个工作setImage:forState:,你需要做以下水平伸缩:myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
在XIB文件中添加一个configuration方法。 你可以select这个选项,如果你想要的文字或图像是在UIButton填充满填充。 这将是相同的代码。
UIButton *btn = [UIButton new]; btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
UIButton *button= [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,70)]; button.buttonType = UIButtonTypeCustom; UIImage *buttonImage = [UIImage imageNamed:@"image.png"]; UIImage *stretchableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:12 topCapHeight:0]; [button setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal];
我发现这个解决scheme。
1) UIButton
的下列方法
+ (id)buttonWithType:(UIButtonType)buttonType { MyButton *toReturn = [super buttonWithType:buttonType]; toReturn.imageView.contentMode = UIViewContentModeScaleAspectFit; return toReturn; } - (CGRect)imageRectForContentRect:(CGRect)contentRect { return contentRect; }
它运作良好。
我遇到了同样的问题,在这个问题上有一个可能的答案:
为什么自定义UIButton图像不能在Interface Builder中resize?
本质上,使用backgroundimage属性,而不是缩放。
奇怪的是,为我工作的唯一组合(iOS 5.1)是…
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
和
[button setImage:newImage forState:UIControlStateNormal];
只要做(从devise或从代码):
从devise
- 打开你的xib或Storyboard。
- selectbutton
- 内部属性检查器 (右侧)>在“控制”部分>水平和Vericalselect最后第四个选项 。
[对于点#3:更改水平和垂直alignment到UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]
从代码
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
像这样可以解决你的问题:
+ (UIImage*)resizedImage:(UIImage*)image { CGRect frame = CGRectMake(0, 0, 60, 60); UIGraphicsBeginImageContext(frame.size); [image drawInRect:frame]; UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resizedImage; }
从小testing,我刚刚读完这里后,取决于如果你使用setImage或setBackgroundImage,都做了相同的结果,strech图像
//for setBackgroundImage self.imageButton.contentMode = UIViewContentModeScaleAspectFill; [self.imageButton setBackgroundImage:[UIImage imageNamed:@"imgFileName"] forState:UIControlStateNormal]; //for setImage self.imageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; self.imageButton.contentVerticalAlignment = UIControlContentVerticalAlignmentFill; [self.imageButton setImage:[UIImage imageNamed:@"imgFileName"] forState:UIControlStateNormal];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill; button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
我无法获得_imageView的解决scheme,但我可以使用CGContextRef
来解决它。 它使用UIGraphicsGetCurrentContext
获取currentContextRef并在currentContextRef中绘制图像,然后缩放或旋转图像并创build一个新图像。 但这并不完美。
代码:
-(UIImage*) scaleAndRotateImage:(UIImage*)photoimage width:(CGFloat)bounds_width height:(CGFloat)bounds_height; { CGImageRef imgRef = photoimage.CGImage; CGFloat width = CGImageGetWidth(imgRef); CGFloat height = CGImageGetHeight(imgRef); CGAffineTransform transform = CGAffineTransformIdentity; CGRect bounds = CGRectMake(0, 0, width, height); bounds.size.width = bounds_width; bounds.size.height = bounds_height; CGFloat scaleRatio = bounds.size.width / width; CGFloat scaleRatioheight = bounds.size.height / height; CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); CGFloat boundHeight; UIImageOrientation orient = photoimage.imageOrientation; switch(orient) { case UIImageOrientationUp: //EXIF = 1 transform = CGAffineTransformIdentity; break; case UIImageOrientationUpMirrored: //EXIF = 2 transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); transform = CGAffineTransformScale(transform, -1.0, 1.0); break; case UIImageOrientationDown: //EXIF = 3 transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationDownMirrored: //EXIF = 4 transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); transform = CGAffineTransformScale(transform, 1.0, -1.0); break; case UIImageOrientationLeftMirrored: //EXIF = 5 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); transform = CGAffineTransformScale(transform, -1.0, 1.0); transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0); break; case UIImageOrientationLeft: //EXIF = 6 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0); break; case UIImageOrientationRightMirrored: //EXIF = 7 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeScale(-1.0, 1.0); transform = CGAffineTransformRotate(transform, M_PI / 2.0); break; case UIImageOrientationRight: //EXIF = 8 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); transform = CGAffineTransformRotate(transform, M_PI / 2.0); break; default: [NSException raise:NSInternalInconsistencyException format:@"Invalid?image?orientation"]; break; } UIGraphicsBeginImageContext(bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { CGContextScaleCTM(context, -scaleRatio, scaleRatioheight); CGContextTranslateCTM(context, -height, 0); } else { CGContextScaleCTM(context, scaleRatio, -scaleRatioheight); CGContextTranslateCTM(context, 0, -height); } CGContextConcatCTM(context, transform); CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageCopy; }
这也将工作,因为backgroundImage自动缩放
[button setBackgroundImage:image forState:UIControlStateNormal];
在屏幕截图的左下方,您可以看到拉伸属性。 尝试使用这些。
故事板
您必须在Identity inspector的 运行时属性中定义特定属性 – rawValue
,其中您将值设置为相对于enum UIViewContentMode
rawValue
位置。 1表示scaleAspectFit 。
和button的alignment,在属性检查器 :
每个UIButton都有一个隐藏的UIImageView。 所以我们必须像下面给出的方式设置内容模式…
[[btn imageView] setContentMode: UIViewContentModeScaleAspectFit]; [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];