我的代码有问题,可以预览带有UIScrollView
. 我的图片来自NSDocumentDirectory
. 虽然我可以从中删除,但是当我从右到左位置开始时,我可以正确地从中删除(就VIEW
& in而言)。NSDocumentDirectory
问题: 现在,无论如何我都可以删除,但是我有一些问题。
首先,虽然我可以删除,但视图没有排列,我的
rearrangeItems:
方法也没有被调用。其次,然后在第一次加载时,我可以删除我喜欢的任何方式,但就像我所说
rearrangeItems:
的方法没有被调用,所以他们的名字不是renamed
。第三,是第一次加载,无论如何我都可以删除,但是当我退出应用程序时,我可以删除但我的图像
NSDocu
没有删除。
希望任何人都可以帮助我。下面是我的代码的预览。
- (void)addImage:(UIImage *)imageToAdd {
[_images addObject:imageToAdd];
[_thumbs addObject:[imageToAdd imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]];
int row = floor(([_thumbs count] - 1) / 5);
int column = (([_thumbs count] - 1) - (row * 5));
UIImage *thumb = [_thumbs objectAtIndex:[_thumbs count]-1];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(column*60+10, row*60+10, 60, 60);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
button.tag = [_images count] - 1;
// This is the title of where they were created, so we can see them move.s
[button setTitle:[NSString stringWithFormat:@"%d, %d", row, column] forState:UIControlStateNormal];
[_buttons addObject:button];
[scrollView addSubview:button];
// This will add 10px padding on the bottom as well as the top and left.
[scrollView setContentSize:CGSizeMake(300, row*60+20+60)];
}
- (void) deleteItem:(id)sender {
_clickedButton = (UIButton *)sender;
UIAlertView *saveMessage = [[UIAlertView alloc] initWithTitle:@""
message:@"DELETE?"
delegate:self
cancelButtonTitle:@"NO"
otherButtonTitles:@"YES", nil];
[saveMessage show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"YES"]) {
NSLog(@"YES was selected.");
UIButton *button = _clickedButton;
[button removeFromSuperview];
[_buttons removeObjectAtIndex:button.tag];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]];
[fileManager removeItemAtPath: fullPath error:NULL];
[self rearrangeItems:button.tag];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[_thumbs removeAllObjects];
for(int i = 0; i <= 100; i++)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
NSLog(@"savedImagePath=%@",savedImagePath);
if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){
[self addImage:[UIImage imageWithContentsOfFile:savedImagePath]];
//NSLog(@"file exists");
}
}
NSLog(@"Count : %d", [_images count]);
}
更新:新的重新排列方法
- (void)rearrangeItems:(int)startIndex {
for (UIButton *button in _buttons) {
// Shift the tags down one
if (button.tag > startIndex) {
NSLog(@"called here");
// Version 2 ****************************
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]];
NSData *imageData = [NSData dataWithContentsOfFile:fullPath];
[fileManager removeItemAtPath: fullPath error:NULL];
// **************************************
button.tag -= 1;
// Version 2 ****************************
fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%lu.png", button.tag]];
[imageData writeToFile:fullPath atomically:YES];
// **************************************
// Recalculate Position
int row = floor(button.tag / 5);
int column = (button.tag - (row * 5));
// Move
button.frame = CGRectMake(column*61+8, row*61+8, 60, 60);
if (button.tag == [_buttons count] - 1) {
[scrollView setContentSize:CGSizeMake(300, row*61+16+60)];
}
}
}
}