【问题标题】:How to flip an image and have it display correctly in all states如何翻转图像并使其在所有状态下正确显示
【发布时间】:2013-01-29 00:08:45
【问题描述】:

我需要翻转和图像,然后将其添加到按钮。我使用了以下代码

UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playButton.frame = CGRectMake(xing, ying, dx,dy);
[playButton setTitle:text forState:UIControlStateNormal];
[playButton addTarget:self action:function forControlEvents:UIControlEventTouchUpInside];



UIImage *buttonImageNormal = [UIImage imageNamed:@"Begin-Set-Button.png"];
UIImage * other = [UIImage imageWithCGImage:buttonImageNormal.CGImage
                    scale:1.0 orientation: UIImageOrientationUpMirrored];
UIImage * awesome = [other stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:awesome forState:UIControlStateNormal];

它可以正确显示图像,但是当我单击它时,按钮会显示未翻转的图像。

为了解决这个问题,我添加了以下代码行

[playButton setBackgroundImage:awesome forState:UIControlStateHighlighted];

然而,当我单击按钮时,它并没有像使用未翻转图像创建的按钮那样使颜色变暗。

如何编写代码,以便当我使用翻转图像时,它显示翻转图像在按下时变暗?我知道如何手动使图像变暗,但我想知道是否有一种方法可以通过简单的函数调用自动完成?

【问题讨论】:

  • 你试过用setImage吗?
  • @Joel 刚刚尝试过。选中后仍显示未翻转的图像。
  • 您是在 IB 中设置按钮 backgroundImage 还是仅在您向我们展示的代码中设置?
  • @Joel 按钮是编码的,所以没有IB。这是我制作按钮的所有代码
  • 今晚我会看看这个,然后回复你。没有承诺,但我会看看它并让你知道!

标签: ios objective-c ios6 uiimage


【解决方案1】:

而不是翻转图像。你试过翻转 imageView 吗?

UIImage *img = [UIImage imageNamed:@"path/to-image.png"];

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.transform = CGAffineTransformMakeRotation(M_PI); // flip the image view
[button setImage:img forState:UIControlStateNormal];

这是我为保持选择状态突出显示而不必处理图像翻转所做的。

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题,所以我搜索了一下这篇文章。我怀疑也许图像没有“固化”,所以镜像操作不是永久的。

    查看“imageWithCGImage:”的文档显示以下内容:

    讨论 此方法不缓存图像对象。您可以使用 Core Graphics 框架的方法来创建 Quartz 图像参考。

    因此,除非在新的上下文中创建新图像,否则图像的镜像不适用于事件。我也一直在研究一种在任意大小的画布上绘制图像以创建具有相同背景的按钮的方法。您可以使用此方法创建一个维护 CG 操作的图像,例如镜像:

    + (UIImage *)imageWithCanvasSize:(CGSize)canvasSize withImage:(UIImage *)anImage
    {
        if (anImage.size.width > canvasSize.width ||
            anImage.size.height > canvasSize.height)
        {
            // scale image first
            anImage = [anImage scaleWithAspectToSize:canvasSize];
        }
    
        CGRect targetRect = CGRectZero;
        CGFloat xOrigin = (canvasSize.width - anImage.size.width) / 2;
        CGFloat yOrigin = (canvasSize.height - anImage.size.height) / 2;
        targetRect.origin = CGPointMake(xOrigin, yOrigin);
        targetRect.size = anImage.size;
    
        UIGraphicsBeginImageContextWithOptions(canvasSize, NO, anImage.scale);
        [anImage drawInRect:targetRect];
    
        UIImage *canvasedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return canvasedImage;    
    }
    
    - (UIImage*)scaleWithAspectToSize:(CGSize)newSize
    {
        CGSize scaledSize = newSize;
    
        float scaleFactor = 1.0;
    
        if (self.size.width > self.size.height)
        {
            scaleFactor = self.size.width / self.size.height;
            scaledSize.width = newSize.width;
            scaledSize.height = newSize.height / scaleFactor;
        }
        else
        {
            scaleFactor = self.size.height / self.size.width;
            scaledSize.height = newSize.height;
            scaledSize.width = newSize.width / scaleFactor;
        }
    
        UIGraphicsBeginImageContextWithOptions(scaledSize, NO, 0.0);
        CGRect scaledImageRect = CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height);
        [self drawInRect:scaledImageRect];
    
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();    
    
        return scaledImage;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2019-08-17
      • 2019-11-30
      • 2022-01-20
      • 2017-09-04
      相关资源
      最近更新 更多