XCODE:更改后如何保存按钮图像?

| 更新 - - 好的,所以我找到了一种方法。...可能不是正确的方法,但是我对xcode还是陌生的,这种方法似乎对我有用。也许有人可以改善这个想法。 最初的问题是更改按钮图像后如何保存?好的,这就是...。我为图像设置了一个自定义按钮,所以我要做的第一件事就是使用以下代码更改该按钮的图像:(顺便说一句,它是为了使您单击一个按钮来更改自定义按钮的图像)
.h file

@interface viewcontroller{

IBOutlet UIButton *btnSetImage1;

}

@property (nonatomic, retain) IBOutlet UIButton *btnSetImage1;
然后是代码:
.m file

@synthesize btnSetImage1;

//Inside a button\'s action

UIImage *btnImage1 = [UIImage imageNamed:@\"button_green.png\"];
        [btnSetImage1 setImage:btnImage1 forState:UIControlStateNormal]; 
-----------------------在哪里回答问题的地方------------------ ------------ 好的,现在在“自定义”按钮上更改了图像,我通过添加以下代码保存了它:
    .h file

@interface viewcontroller{

IBOutlet UIButton *btnSetImage1;
IBOutlet UIImageView *buttonImgChange1;

}

@property (nonatomic, retain) IBOutlet UIButton *btnSetImage1;
@property (nonatomic, retain) IBOutlet UIImageView *buttonImgChange1;
然后是代码:
.m file

@synthesize btnSetImage1;
@synthesize buttonImgChange1;

//Inside a button\'s action

UIImage *btnImage1 = [UIImage imageNamed:@\"button_green.png\"];
        [btnSetImage1 setImage:btnImage1 forState:UIControlStateNormal]; 

//Set image to variable  

buttonImgChange1 = [[UIImageView alloc] initWithImage:btnImage1];

        //SAVE CHANGES

        // Create instances of NSData
        UIImage *savedButtonImage1 = buttonImgChange1.image;
        NSData *buttonData1 = UIImagePNGRepresentation(savedButtonImage1);    
        // Store the data
        NSUserDefaults *buttondefaults1 = [NSUserDefaults standardUserDefaults];
        [buttondefaults1 setObject:buttonData1 forKey:@\"buttonImage1\"];

        [buttondefaults1 synchronize];
这样,我就可以将按钮图像保存为名为““ buttonImage1 \”的键。然后我只是在viewdidload中调用了这个键:
//-----------UPDATE BUTTONS---------------


// Get the stored data before the view loads
NSUserDefaults *buttondefaults1 = [NSUserDefaults standardUserDefaults];

NSData *buttonData1 = [buttondefaults1 dataForKey:@\"buttonImage1\"];
UIImage *savedButtonImage1 = [UIImage imageWithData:buttonData1];


// Update the UI elements with the saved data
buttonImgChange1.image = savedButtonImage1;


//Update buttons
UIImage *btnImage1 = savedButtonImage1;
[btnSetImage1 setImage:btnImage1 forState:UIControlStateNormal];

buttonImgChange1 = [[UIImageView alloc] initWithImage:btnImage1];
就像我说的...我是xcode的新手,并四处寻找解决这个问题的方法。在自己摸索之后,我想到了这个解决方案。我不知道这可能不是最好或最有效的方法,但它对我有用,我希望它能在整个过程中对其他人有所帮助。 哦,还有一条便条,如果您需要两个不同的图像,一个图像显示按钮的正常状态,然后在单击按钮时显示另一个图像,只需复制所有代码并重命名所有变量,forKey名称,最后将forState更改为-forState:UIControlStateHighlighted。有了按钮,按下该按钮即可更改并保存自定义按钮的图像。 谢谢!     
已邀请:

要回复问题请先登录注册