在NSDocumentDirectory和UIScrollView中删除 [英] Deleting in NSDocumentDirectory and UIScrollView
问题描述
我的代码出现问题,能够在 UIScrollView
中预览包含图像的缩略图。我的图片来自 NSDocumentDirectory
。我可以删除它,但我可以删除它(在 VIEW
& in NSDocumentDirectory
)正确我从右到左的位置开始。
Having a problem with my code, was able to have a preview with thumbnail with images in a UIScrollView
. My images is from NSDocumentDirectory
. I can delete from it though but I can delete from it (in terms of VIEW
& in NSDocumentDirectory
) PROPERLY when I start from the right to left position.
问题:
现在,无论如何我都可以删除,但我有几个问题。
PROBLEM: Now, I can delete anyway, But I have some several problems.
-
首先,虽然我可以删除,但视图没有排列,我的
rearrangeItems:
方法也没有被调用。
First, though I can delete, the view is not arranging, my
rearrangeItems:
method is not being called also.
第二次,然后在第一次加载时我无论如何都可以删除,但就像我说的那样 rearrangeItems :
方法未被调用,因此他们的名字
不是重命名
。
Second,Then at first load I can delete anyway I like, but like what I said rearrangeItems:
method is not being called, so their names
arent renamed
.
第三次,是第一次加载,我可以删除,但是当我退出应用程序时,我可以删除但是我的图像在 NSDocu
没有删除。
Third, is at first load, I can delete in anyway, but when I exit the app, I can delete but my images in the NSDocu
is not deleting.
希望有人可以帮我解决这个问题。下面是我的代码的预览。
Hope anyone could help me with this. Below is the preview of my code.
- (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)];
}
}
}
}
推荐答案
你显示rearrangeButtons但从不使用它 - 我认为上面的代码已经过时了。在任何情况下,你都有一些小问题:
You show "rearrangeButtons" but never use it - I assume the code above is out of date. In any case you have some small issues here:
[_images objectAtIndex:button.tag];
[_images removeObjectAtIndex:button.tag];
[_images removeObject:button];
第一个和最后一个语句没有意义,你应该使用的是:
The first and last statements make no sense, what you should be using is:
[_images removeObjectAtIndex:button.tag];
[self rearrangeButtons:button.tag];
要向应用添加完整性检查,请尝试将此代码添加到rearrangeButtons的末尾:
To just add a sanity check to the app, try adding this code to the end of rearrangeButtons:
int idx = 0;
for (UIButton *button in _buttons) {
NSLog(@"Going to query button at index %d", idx);
NSLog(@"Button at index %d is of type %@", idx, NSStringFromClass([button class]);
// if the button is not a UIView subclass, it won't have tag. If its a dealloced
// object then it probably will crash when you ask it its class...
if(button.tag != idx) NSLog(@"INDEX PROBLEM AT BUTTON ARRAY INDEC %d", idx);
++idx;
}
编辑:在循环中编辑的代码打印出对象类
code edited in loop to print out the object class
EDIT2:所以我把你的代码放到一个新项目中, ButtonManager 。基本上没问题,但是你遇到了一些问题。首先,即使没有文件,你也可以索引文件名,所以索引可能不同步。其次,你使用%lu格式的button.tag,但这是一个整数所以你应该使用%d。最后,你删除数组中的按钮,但不删除图像或缩略图。
So I took your code and put it into a new project, ButtonManager. Essentially its OK but you have a few problems. First, you index the file names even if no file exists, so the indexes could get out of sync. Second, you use %lu format for a button.tag, but that is an integer so you should be using "%d". Lastly, you delete buttons from the array but not the images or the thumbnails.
如果您下载该项目,您会看到在需要更改以便正常工作的所有地方发出警告。我不确定为什么按钮索引被破坏 - 可能是其他代码。在任何情况下都会在代码中添加一个数据一致性测试 - 在代码中调用它 - 如果它失败了那么你知道你的问题是在最后一个好的测试和最新的失败测试之间。
If you download that project, you will see warnings in all the places you need to change to get to work properly. I am not sure why the button indexes got corrupted - maybe other code. In any case there is a data consistency test added to the code - sprinkle calls to that around in your code - if its ever fails then you know your problem is between the last good test and the latest failing one.
该项目启动时会启动deleteItem:消息,只要点击是,就会一直删除数组中间的项目。
The project kicks off a "deleteItem:" message when it starts, and just keeps deleting items in the middle of the array as long as you tap YES.
这篇关于在NSDocumentDirectory和UIScrollView中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!