好的,所以我试图让用户从 iphone 照片中选择图像并将该图像保存为按钮上的图像。当用户关闭应用程序并再次打开它时,我希望用户再次看到他保存的图像:) theButton 包含单击哪个按钮的发送者。
我可以做用户可以将图像作为按钮图像的部分。我用这条线来设置图像
[theButton setImage:btnImage forState:UIControlStateNormal];
现在我想知道如何访问该按钮的图像?所以我可以保存和加载它吗?
我根据教程使用此代码,但我被加载部分卡住了,因为显然我不必使用 Imageview,因为它只是可读的:) 我的代码:
//SAVE
-(NSString *)pathOfFile{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolder = [paths objectAtIndex:0];
return [documentsFolder stringByAppendingFormat:@"myfile.plist"];
}
-(void)applicationWillTerminate:(NSNotification*)notification{
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject: tileOne.imageView];
[array addObject: tileTwo.imageView];
[array writeToFile:[self pathOfFile] atomically:YES];
}
//LOAD
- (void)viewDidLoad
{
NSString *filePath = [self pathOfFile];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
//Here it says read only, what to use then?
tileOne.imageView = [array objectAtIndex:0];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
[super viewDidLoad];
}