UIButton图像旋转问题

UIButton图像旋转问题,uibutton,uiimage,Uibutton,Uiimage,我有一个[UIButton Button WithType:UIButtonTypeCustom],它有一个图像(或背景图像-同样的问题),由[UIImage imageWithContentsOfFile::创建,指向相机拍摄并由应用程序保存在documents文件夹中的JPG文件 如果我仅为UIControlStateNormal定义图像,则当我触摸按钮时,图像会像预期的那样变暗,但它也会旋转90度或180度。当我取下手指时,它会恢复正常 如果我对uicontrol状态高亮显示使用相同的图像

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

如果我仅为
UIControlStateNormal
定义图像,则当我触摸按钮时,图像会像预期的那样变暗,但它也会旋转90度或180度。当我取下手指时,它会恢复正常

如果我对
uicontrol状态高亮显示
使用相同的图像,但随后我失去触摸指示(较暗的图像),则不会发生这种情况

这仅在从文件读取图像时发生。这不会发生在
[UIImage ImageNamed::

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


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

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

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

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
}

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