在我的应用程序中,当按下按钮 n 自定义imagePicker
出现时,它包含我的图像NSDocumentDirectory
,我想要的是当我选择一个图像时,它会:隐藏、删除、设置为选择,或者任何认为它被选中的东西。
我尝试过为它分配一个整数,所以下次我展示它时,imagePicker
它会执行上述任一操作,但仍然没有运气。
您能否建议我如何解决我的问题或任何建议。谢谢你。
这是我的代码:
- (void)viewDidLoad {
// Create view
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count; ++i) {
UIImage *thumb = [_thumbs objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[view addSubview:button];
if (column == 6) {
column = 0;
row++;
} else {
column++;
}
}
[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
self.view = view;
[super viewDidLoad];
}
这是我尝试设置它的地方:
- (void) viewDidAppear:(BOOL)animated{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImage = [prefs integerForKey:@"selected"];
if ( selectedImage ){
[myButton removeFromSuperview];
[myButton setBackgroundImage:[UIImage imageNamed:@"Checkmark.png"] forState:UIControlStateSelected];
[myButton setSelected:YES];
[myButton setHidden:YES];
[myButton setHighlighted:YES];
myButton.selected = YES;
}
}
点击时:
- (IBAction)buttonClicked:(id)sender {
self.myButton = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:myButton.tag];
[[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:@"selected"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self dismissModalViewControllerAnimated:NO];
}
调用imagePicker
:
- (IBAction)chooseCustomImageTapped:(id)sender {
_imagePicker1 = [[CustomImagePicker alloc] init];
_imagePicker1.title = @"Choose Custom Image";
for(int i = 0; i <= 30; i++)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){
[_imagePicker1 addImage:[UIImage imageWithContentsOfFile:savedImagePath]];
}
}
[self presentModalViewController:_imagePicker1 animated:NO];
}