KON*_*ONG 87 cocoa-touch objective-c uibutton uikit ios
我想将图像添加到UIButton,还想缩放我的图像以适应UIButton(使图像更小).请告诉我该怎么做.
这是我尝试过的,但它不起作用:
setContentMode:[self.itemImageButton setImage:stretchImage forState:UIControlStateNormal];
[self.itemImageButton setContentMode:UIViewContentModeScaleAspectFit];
Run Code Online (Sandbox Code Playgroud)
UIImage *stretchImage = [updatedItem.thumbnail stretchableImageWithLeftCapWidth:0 topCapHeight:0];
Run Code Online (Sandbox Code Playgroud)
Dav*_*ave 198
我有同样的问题.只需设置UIButton内的ImageView的ContentMode即可.
[[self.itemImageButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[self.itemImageButton setImage:[UIImage imageNamed:stretchImage] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
nal*_*exn 101
这里没有任何答案对我有用,我用以下代码解决了这个问题:
button.contentMode = UIViewContentModeScaleToFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)
您也可以在Interface Builder中执行此操作.
Tan*_* G. 48
在宽高比适配模式下以编程方式设置UIButton imageView的最简单方法:
迅速
button.contentHorizontalAlignment = .fill
button.contentVerticalAlignment = .fill
button.imageView?.contentMode = .scaleAspectFit
Run Code Online (Sandbox Code Playgroud)
Objective-C的
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
Run Code Online (Sandbox Code Playgroud)
注意:您可以将.scaleAspectFit(UIViewContentModeScaleAspectFit)更改为.scaleAspectFill(UIViewContentModeScaleAspectFill)以设置方面填充模式
gca*_*amp 28
如果您真的想要缩放图像,请执行此操作,但在使用之前应调整大小.在运行时调整它的大小只会丢失CPU周期.
这是我用来缩放图像的类别:
的UIImage + Extra.h
@interface UIImage (Extras)
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize;
@end;
Run Code Online (Sandbox Code Playgroud)
的UIImage + Extra.m
@implementation UIImage (Extras)
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (!CGSizeEqualToSize(imageSize, targetSize)) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil) NSLog(@"could not scale image");
return newImage ;
}
@end
Run Code Online (Sandbox Code Playgroud)
您可以使用它想要的尺寸.喜欢 :
[self.itemImageButton setImage:[stretchImage imageByScalingProportionallyToSize:CGSizeMake(20,20)]];
Run Code Online (Sandbox Code Playgroud)
nin*_*eer 19
我有问题图像没有按比例调整大小,所以我修复它的方式是使用边缘插入.
fooButton.contentEdgeInsets = UIEdgeInsetsMake(10, 15, 10, 15);
Run Code Online (Sandbox Code Playgroud)
Ort*_*ntz 14
扩展Dave的答案,您可以使用运行时属性在IB中设置contentMode按钮imageView全部,而无需任何代码:
1意思是UIViewContentModeScaleAspectFit,2意思是
UIViewContentModeScaleAspectFill.如果您只想减少按钮图像:
yourButton.contentMode = UIViewContentModeScaleAspectFit;
yourButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
Run Code Online (Sandbox Code Playgroud)
我有一个方法可以为我做这件事。该方法采用UIButton并使图像方面适合。
-(void)makeImageAspectFitForButton:(UIButton*)button{
button.imageView.contentMode=UIViewContentModeScaleAspectFit;
button.contentHorizontalAlignment=UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment=UIControlContentVerticalAlignmentFill;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
雨燕5.0
myButton2.contentMode = .scaleAspectFit
myButton2.contentHorizontalAlignment = .fill
myButton2.contentVerticalAlignment = .fill
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
109732 次 |
| 最近记录: |