Ios 更改UIImageView';s按文件名显示颜色

Ios 更改UIImageView';s按文件名显示颜色,ios,uiimageview,Ios,Uiimageview,我正在尝试更改UIImageView颜色,颜色更改代码工作正常,但我的问题是该方法应根据其文件名动态更改图像的颜色: 更改帧图像: - (IBAction)fr1:(id)sender { mainViewCont.frameImage.image = [UIImage imageNamed:@"sf1.png"]; } - (IBAction)fr2:(id)sender { mainViewCont.frameImage.image = [UIImage imageNam

我正在尝试更改UIImageView颜色,颜色更改代码工作正常,但我的问题是该方法应根据其文件名动态更改图像的颜色:

更改帧图像:

- (IBAction)fr1:(id)sender {

    mainViewCont.frameImage.image = [UIImage imageNamed:@"sf1.png"];

}
- (IBAction)fr2:(id)sender {
    mainViewCont.frameImage.image = [UIImage imageNamed:@"sf2.png"];

}
更改色调颜色:

- (IBAction)frameColor:(UISegmentedControl *)sender {

    NSString *imageName ;

    for( int i = 0; i < 44 ; i++ ) {

        imageName = [NSString stringWithFormat:@"sf%d.png",i];


    switch (sender.selectedSegmentIndex) {

        case 0:
            mainViewCont.frameImage.image = [tintImage imageNamed:imageName withColor:[UIColor blackColor]];
            break;

        case 1:
               mainViewCont.frameImage.image = [tintImage imageNamed:imageName withColor:[UIColor whiteColor]];
            break;

        default:
            break;
        }

    }
}
-(iAction)frameColor:(UISegmentedControl*)发送方{
NSString*imageName;
对于(int i=0;i<44;i++){
imageName=[NSString stringWithFormat:@“sf%d.png”,i];
开关(发送方。选择的分段索引){
案例0:
mainViewCont.frameImage.image=[tintImage imageName:imageName withColor:[UIColor blackColor]];
打破
案例1:
mainViewCont.frameImage.image=[tintImage imageName:imageName withColor:[UIColor whiteColor]];
打破
违约:
打破
}
}
}

现在的问题是只有图像的色调有44个数字(最后一张图像)改变!!当我按frame 24时,它无法识别帧图像是数字24。

在循环中,您正在设置UIImageView的image属性44次。这意味着您只能看到最后一张图像。您只需更改已设置为图像的图像的颜色

您应该向ViewController添加两个实例变量<代码>图像颜色和
图像名称

您必须添加名为
configureView
的方法,该方法使用
imageName
加载图像并设置其
tintColor

在IBAction中选择图像,将图像名称保存在
imageName
中,然后调用
configureView
。在分段控制操作中,将所选颜色保存在
imageTintColor
中,并调用
configureView

基本上是这样的:

- (void)configureView {
    UIImage *image = [tintImage imageNamed:self.imageName withColor:self.imageTintColor];
    _frame.image = image;
}

- (IBAction)img1:(id)sender {
    self.imageName = @"sf1.png";
    [self configureView];
}

- (IBAction)img2:(id)sender {
     self.imageName = @"sf2.png";
    [self configureView];
}

- (IBAction)img3:(id)sender {
    self.imageName = @"sf3.png";
    [self configureView];
}

- (IBAction)changeColor:(UISegmentedControl *)sender {
    NSString *imageName ;
    for( int i = 1; i <= 3 ; i++ ) {

        imageName = [NSString stringWithFormat:@"sf%d.png",i];

        switch (sender.selectedSegmentIndex) {
            case 0:
                self.imageTintColor = [UIColor blackColor];
                break;

            case 1:
                self.imageTintColor = [UIColor whiteColor];
                break;

            default:
                break;
        }
    }
    [self configureView];
}
-(无效)配置视图{
UIImage*image=[tintImage imageName:self.imageName withColor:self.imageTintColor];
_frame.image=图像;
}
-(iAction)img1:(id)发送方{
self.imageName=@“sf1.png”;
[自配置视图];
}
-(iAction)img2:(id)发送方{
self.imageName=@“sf2.png”;
[自配置视图];
}
-(iAction)img3:(id)发送方{
self.imageName=@“sf3.png”;
[自配置视图];
}
-(iAction)changeColor:(UISegmentedControl*)发送方{
NSString*imageName;

对于(int i=1;我知道loop在这里做什么吗?
imageName
正在被重写,最后它将包含
imageName=sf43.png
@我编辑了帖子和代码,但没有任何更改。问题不清楚,但你可以使用标签,不明白为什么要使用loop。当然,它只更改了最后一个在您的循环中,您将覆盖frameImage(可能是UIImageView)的image属性44次。这没有多大意义。