UIButton图像旋转问题

11

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

如果仅为UIControlStateNormal定义图像,则当我触摸按钮时,预期会使图像变暗,但它还会旋转90度或180度。当我松开手指时,它会恢复正常。

如果我将同一图像用于UIControlStateHighlighted,则不会发生这种情况,但我会失去触摸指示(变暗的图像)。

只有从文件中读取的图像才会出现这种情况。使用[UIImage ImageNamed:]则不会发生这种情况。

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

这是一个bug还是我的操作有误?


2
我有一个类似的问题,即在将取消按钮背景图像设置为从uiimagepicker选择的图片后,图像会逆时针旋转90度。 - marciokoko
2个回答

4

我无法找到一个合适的解决方案,因此需要一个快速的解决方法。以下是一个函数,给定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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接