【问题标题】:UIButton image rotation issueUIButton图像旋转问题
【发布时间】:2011-08-29 13:23:07
【问题描述】:

我有一个[UIButton buttonWithType:UIButtonTypeCustom],它有一个由[UIImage imageWithContentsOfFile:] 创建的图像(或背景图像 - 相同的问题),指向一个由相机拍摄并由应用程序保存在文档文件夹中的 JPG 文件。

如果我只为UIControlStateNormal 定义图像,那么当我触摸按钮时,图像会按预期变暗,但它也会旋转 90 度或 180 度。当我移开手指时,它会恢复正常。

如果我对UIControlStateHighlighted 使用相同的图像,则不会发生这种情况,但我会失去触摸指示(较暗的图像)。

这只发生在从文件中读取图像的情况下。 [UIImage ImageNamed:] 不会发生这种情况。

我尝试将文件保存为 PNG 格式而不是 JPG。在这种情况下,图像一开始就以错误的方向显示,并且在触摸时不会再次旋转。无论如何,这不是一个好的解决方案,因为 PNG 太大而且处理起来很慢。

这是一个错误还是我做错了什么?

【问题讨论】:

  • 我有一个类似的问题,在将取消按钮背景图像设置为从 uiimagepicker 中选择的图片后,图像显示为逆时针旋转 90 度。

标签: uibutton uiimage


【解决方案1】:

我无法找到适当的解决方案,我需要一个快速的解决方法。下面是一个函数,给定一个 UIImage,它返回一个用深色 alpha 填充变暗的新图像。上下文填充命令可以替换为其他绘制或填充例程,以提供不同类型的变暗。

这是未经优化的,是在对图形 api 了解最少的情况下制作的。

你可以使用这个函数来设置 UIControlStateHighlighted 状态图像,这样至少它会更暗。

+ (UIImage *)darkenedImageWithImage:(UIImage *)sourceImage
{
    UIImage * darkenedImage = nil;

    if (sourceImage)
    {
        // drawing prep
        CGImageRef source = sourceImage.CGImage;
        CGRect drawRect = CGRectMake(0.f,
                                     0.f,
                                     sourceImage.size.width, 
                                     sourceImage.size.height);
        CGContextRef context = CGBitmapContextCreate(NULL, 
                                                     drawRect.size.width, 
                                                     drawRect.size.height,
                                                     CGImageGetBitsPerComponent(source),
                                                     CGImageGetBytesPerRow(source),
                                                     CGImageGetColorSpace(source),
                                                     CGImageGetBitmapInfo(source)
                                                     );

        // draw given image and then darken fill it
        CGContextDrawImage(context, drawRect, source);
        CGContextSetBlendMode(context, kCGBlendModeOverlay);
        CGContextSetRGBFillColor(context, 0.f, 0.f, 0.f, 0.5f);
        CGContextFillRect(context, drawRect);

        // get context result
        CGImageRef darkened = CGBitmapContextCreateImage(context);
        CGContextRelease(context);

        // convert to UIImage and preserve original orientation
        darkenedImage = [UIImage imageWithCGImage:darkened
                                                      scale:1.f
                                                orientation:sourceImage.imageOrientation];
        CGImageRelease(darkened);
    }

    return darkenedImage;
}

【讨论】:

    【解决方案2】:

    要解决此问题,您需要额外的标准化函数,如下所示:

    public extension UIImage {
    
        func normalizedImage() -> UIImage! {
    
            if self.imageOrientation == .Up {
                return self
            }
    
            UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
            self.drawInRect(CGRectMake(0, 0, self.size.width, self.size.height))
            let normalized = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
    
            return normalized
        }
    }
    

    那么你可以这样使用它:

    self.photoButton.sd_setImageWithURL(avatarURL, 
    forState: .Normal, 
    placeholderImage: UIImage(named: "user_avatar_placeholder")) {
    
        [weak self] (image, error, cacheType, url) in
    
        guard let strongSelf = self else {
    
            return
        }
    
        strongSelf.photoButton.setImage(image.normalizedImage(), forState: .Normal
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      相关资源
      最近更新 更多