简体   繁体   English

如果在didMoveToSuperview中使用,则UIImage + Retina4类别不起作用

[英]UIImage+Retina4 category not working if used in didMoveToSuperview

I'm using this UIImage category to automatically find the correct asset if the app's running on a retina 4 device: http://www.sourcedrop.net/FY53a14b0127f 如果应用程序在Retina 4设备上运行,我正在使用此UIImage类别自动查找正确的资产: http : //www.sourcedrop.net/FY53a14b0127f

It correctly finds the asset with the -568h@2x suffix if the UIImage is instantiated in a UIView subclass's init method: 如果在UIView子类的init方法中实例化了UIImage则可以正确找到带有-568h@2x后缀的资产:

-(id) init{
    self = [super init];
    if(self){
        myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [myButton setImage:[UIImage imageNamed:@"go_back_image"] forState:UIControlStateNormal];
        [self addSubview:myButton];
    }
}

but if the UIImage is instantiated in the class's didMoveToSuperView then the category doesn't pick up the asset: 但是,如果UIImage在类的didMoveToSuperView实例化,则该类别不会获取资产:

-(void)didMoveToSuperview{
    if(self.superview != nil){
            [myButton setImage:[UIImage imageNamed:@"otherImage"] forState:UIControlStateNormal];
            [myButton setImage:[UIImage imageNamed:@"otherImageHighlighted"] forState:UIControlStateHighlighted];
    }
}

If the UIImage is created in didMoveToSuperview then the normal size asset is shown... 如果UIImage是在didMoveToSuperview创建的,则显示正常大小的资产...

Any thoughts? 有什么想法吗?

Try working with breakpoints in the category code. 尝试使用类别代码中的断点。

I had an issue where an image wasn't picked because I set an image as @"someImage.png" like 我遇到一个问题,因为我将图像设置为@“ someImage.png”,所以无法选择图像

[myButton setImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];

instead of simply "someImage" like so 而不是像这样简单地“ someImage”

[myButton setImage:[UIImage imageNamed:@"someImage"] forState:UIControlStateNormal];

Found this was the issue by debugging in the category class and used this one line to fix: 通过在类别类中进行调试发现了问题所在,并使用这一行来修复:

//removing png extension, if present
imageNameMutable = (NSMutableString *)[imageNameMutable stringByDeletingPathExtension];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:@"png"];

You might have to do something similar. 您可能需要做类似的事情。 Best of luck :) 祝你好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM