将UIImage放入UIButton的简单方法

我的iPhone应用程序中有一个UIButton。我将其大小设置为100x100。 我有一个400x200的图像,我希望在按钮中显示。 按钮STILL需要保持在100x100 ......我希望图像缩小以适应......但是 保持正确的宽高比。 我认为这就是“Aspect Fit”的用途。 我是否使用setImage或setBackgroundImage包含我的图像? 我是否为按钮设置了AspectFit?还是形象?还是背景图片? (我需要较小的图像来增加大小。虽然较大的图像应该是DESCREASE的大小。 始终保持按钮为100x100。) 我已经尝试了以上所有的无数组合......而我似乎无法得到 一切都在同一时间工作: 请勿将按钮的大小更改为100x100。 不要破坏图像的纵横比。 始终增加小图像以适合按钮。 始终减小大图像以适合按钮。 切勿剪辑任何图像边缘。 永远不要求“将UIButtons放在你所有的UIimages上”黑客攻击。 不要求每个人都只是为了使用新的框架方法而升级到v4.2.1。 我看到很多应用程序,有如此多的全功能图像按钮...我无法相信我无法弄清楚这个非常简单,非常常见的事情。 啊。     
已邀请:
UIButton坏了。这是简短的答案。其图像和backgroundImage属性中的
UIImageView
s不符合
UIViewContentMode
设置。它们是只读属性,虽然这些UIImageViews的
UIImage
内容可以通过
setImage:
setBackgroundImage:
方法设置,但内容模式不能。 解决方案是要么在您的包中提供适当大小的图像,要么放下UIImageView,按照您希望的方式配置它,然后在其上面放置一个清晰的“自定义”UIButton。我保证,这就是你见过的那些花哨的专业应用程序。我们都必须这样做。     
UIImage *img = [UIImage imageNamed:@"yourImageName"];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
[button setImage:img forState:UIControlStateNormal];
    
为了正确地执行此操作,我实际上将以编程方式调整大小并操作图像以获得所需的宽高比。这避免了对任何视图层次结构黑客的需要,并且还减少了对单个操作的任何性能影响,而不是每次重绘。 这个(未经测试的)代码应该有助于说明我的意思:
CGSize imageSize = image.size;
CGFloat currentAspect = imageSize.width / imageSize.height;

// for purposes of illustration
CGFloat targetWidth = 100;
CGFloat targetHeight = 100;
CGFloat targetAspect = targetWidth / targetHeight;

CGFloat newWidth, newHeight;

if (currentAspect > targetAspect) {
    // width will end up at 100, height needs to be smaller
    newWidth = targetWidth;
    newHeight = targetWidth / currentAspect;
} else {
    // height will end up at 100, width needs to be smaller
    newHeight = targetHeight;
    newWidth = targetHeight * currentAspect;
}

size_t bytesPerPixel = 4;

// although the image will be resized to { newWidth, newHeight }, it needs
// to be padded with empty space to provide the aspect fit behavior
//
// use calloc() to clear the data as it's allocated
void *imageData = calloc(targetWidth * targetHeight, bytesPerPixel);

if (!imageData) {
    // error out
    return;
}

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if (!colorSpace) {
    // error out
    return;
}

CGContextRef context = CGBitmapContextCreate(
    imageData,
    targetWidth,
    targetHeight,
    8, // bits per component
    targetWidth * bytesPerPixel, // bytes per row
    colorSpace,
    kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst
);

CGColorSpaceRelease(colorSpace);

// now we have a context to draw the original image into
// in doing so, we want to center it, so prepare the geometry
CGRect drawRect = CGRectMake(
    floor((targetWidth - newWidth) / 2),
    floor((targetHeight - newHeight) / 2),
    round(newWidth),
    round(newHeight)
);

CGContextDrawImage(context, drawRect, image.CGImage);

// now that the bitmap context contains the aspect fit image with transparency
// letterboxing, we want to pull out a new image from it
CGImageRef newImage = CGBitmapContextCreateImage(context);

// destroy the temporary context
CGContextRelease(context);
free(imageData);

// and, finally, create a new UIImage
UIImage *newUIImage = [UIImage imageWithCGImage:newImage];
CGImageRelease(newImage);
如果有任何部分不清楚,请告诉我。     
因为我的尝试都没有奏效.... 也许我应该问这个。使用UIButton时: 我何时使用setImage而不是setBackgroundImage? (为什么两者都有?) 我什么时候使用“Aspect Fit”而不是“Center”? (当我期望它们“保持纵横比”和“不调整任何东西”时,为什么两者似乎都会拉伸我的图像。) 而最大的问题是:为什么这么常见?这么大的混乱? 如果我能找到一种类似的解决方法,它将全部解决:只需使用UIImage并检测TAPS。 (但这似乎甚至是一个更大的代码噩梦。) 苹果公司,如果你试图让我的工作变得更轻松......你反而把它变得混乱了400倍。     
我认为丹试图说的是(但没有说过)是这样做的: 使用“临时图像”为您调整大小。 温度图像需要设置为ASPECT FIT和HIDDEN。 确保您的按钮设置为所需的大小,而不是设置为ASPECT FIT。 //使框架与按钮的尺寸相同 CGRect aFrame = CGRectMake(0,0,myButton.frame.size.width,myButton.frame.size.height); //将临时图像设置为按钮的大小 imgTemp.frame = aFrame; //将图像放入临时图像 imgTemp.image = anImage; //将调整大小的临时图像复制到按钮 [myButton setBackgroundImage:tempImage forState:UIControlStateNormal];     
在按钮上放置一个imageview,为imageview设置图像而不是按钮。 祝一切顺利。     
我会将图像调整为100x100,保持图像中包含的内容的宽高比。然后将UIButton的backgroundImage属性设置为图像。     
几天前我遇到同样的问题并解决了。请试试这个
[_profilePicBtn setImage:profilePic forState:UIControlStateNormal];
_profilePicBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    

要回复问题请先登录注册