2010-12-21 97 views
12

我的iPhone应用程序中有一个UIButton。我将它的大小设置为100x100。将UIImage放入UIButton的简单方法

我有一个400x200的图像,我希望在按钮中显示。

按钮仍然需要保持在100x100 ...我希望图像缩小以适合...但 保持正确的宽高比。

我认为这就是“Aspect Fit”的用法。

是否包含我的图像与setImage或setBackgroundImage?

我是否为该按钮设置了AspectFit?或图像?或背景图像?

(我需要更小的图像尺寸的增加。虽然较大的图像的大小是否应当DESCREASE。 始终保持按钮100×100)。

我已经尝试了所有上述的无数组合...我似乎无法得到 一切工作在同一时间:

  • 不要更改按钮的大小100x100。
  • 不要破坏图像的宽高比。
  • 总是增加小图像以适应按钮。
  • 始终减小大图以适应按钮。
  • 切勿剪切任何图像边缘。
  • 永远不要求“把UIButtons放在你所有的UIimages上”。
  • 不要求每个人都升级到v4.2.1只是使用新的框架方法。

我看到这么多的应用程序,有这么多全功能的图像按钮......我不能相信我无法弄清楚这个非常简单,很常见的事情。

呃。

回答

1

在按钮上放置一个imageview,为imageview设置图像而不是按钮。

一切顺利。

+0

实际上,我会把'UIImageView` **放在**按钮下,并使按钮“自定义”,因此它是不可见的。 – 2010-12-21 17:42:52

+2

我想要做到“正确”,并避免25个按钮的攻击......超过25张图片......必须创建50个对象以及所有内存,维护,插座,操作等等。所有功能强大,内置的按钮方法...如果每个人的解决方案是“只是不使用它们”? – Alice 2010-12-21 20:19:26

1

我会将图像大小调整为100x100,以保持图像中包含的内容的宽高比。然后将UIButton的backgroundImage属性设置为图像。

+1

这正是我需要做的......但*如何*? (当别人的'回答'是“只是做”时,你不喜欢它...而不是*如何做*)如何调整图像大小?我如何保持它的长宽比? (请记住,我不想将图像大小调整为100x100 ...我希望在100x100范围内具有FIT *(无论其原始大小或新大小)...只要其宽度(或高度)变成完全是100. – Alice 2010-12-21 20:23:06

+0

有很多方法来调整图像的大小,例如http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage您是否试图简单地创建一个UIImageView作为按钮的子视图?我不确定它是否可以工作,但它似乎是最简单的方法,如果它可以工作的话。 – 2010-12-21 21:19:59

+0

我不能合法的,没有记录的东西。我不能强迫每个人更新使用v4.2.1框架。 – Alice 2010-12-21 21:47:15

2

我觉得丹是想说(但没有以往任何时候都称它)是这样做:

  • 使用“临时图像”做调整大小为您服务。
  • 需要将temp-image设置为ASPECT FIT和HIDDEN。
  • 确保您的按钮设置为您所需的尺寸,而不是设置为ASPECT FIT。
 
// Make a frame the same size as your button 
CGRect aFrame = CGRectMake(0, 0, myButton.frame.size.width, myButton.frame.size.height); 

// Set your temp-image to the size of your button 
imgTemp.frame = aFrame; 

// Put your image into the temp-image 
imgTemp.image = anImage; 

// Copy that resized temp-image to your button 
[myButton setBackgroundImage:tempImage forState:UIControlStateNormal]; 
12

的UIButton坏了。这是简短的答案。其图像和背景图像属性中的UIImageView s不尊重UIViewContentMode设置。他们只读属性,而那些的UIImageViews的UIImage内容可以通过setImage:setBackgroundImage:方法来设置,内容模式是不可能的。

解决方案是在您的软件包中提供适当大小的图像,或者放下UIImageView,按照需要的方式进行配置,然后在上面放置一个清晰的“自定义”UIButton。我承诺,这就是所有你看过的那些奇特的专业应用程序所使用的。我们都必须这样做。

4

为了正确地做到这一点,我真的编程调整大小和操纵图像以获得所需的纵横比。这避免了任何视图层次结构的需求,并且也减少了对单个操作的任何性能影响,而不是每次重绘。

这(未经测试)的代码应该有助于说明我的意思:

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

让我知道的任何部分还不清楚。

2

由于没有我的尝试工作过....

也许我应该不是问这个。当使用UIButton时:

DO我使用setImage而不是setBackgroundImage? (为什么都有?)

DO我用“Aspect Fit”而不是“Center”? (为什么双方似乎当我希望他们“保持宽高比”和“不调整任何”伸展我的图片,分别。)

而最大的问题:为什么是这样一个共同的东西......这样的一个巨大的混乱?

它会如果我能找到像一个变通的方法都可以解决:只需使用UIImage的替代和检测TAPS。 (不过,这似乎是连码LARGER噩梦。)

苹果,如果你试图让我的工作变得更简单...你不是做400次更加混乱。

6
UIImage *img = [UIImage imageNamed:@"yourImageName"]; 
button.imageView.contentMode = UIViewContentModeScaleAspectFit; 
[button setImage:img forState:UIControlStateNormal]; 
0

我几天前面临同样的问题并解决它。请尝试这个

[_profilePicBtn setImage:profilePic forState:UIControlStateNormal]; 
_profilePicBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;