我的代码有问题,所以可以用缩略图预览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)];
}
}
}
}发布于 2012-08-30 11:46:28
您显示的是"rearrangeButtons“,但永远不要使用它--我认为上面的代码已经过时了。在任何情况下,这里都有一些小问题:
[_images objectAtIndex:button.tag];
[_images removeObjectAtIndex:button.tag];
[_images removeObject:button];第一个和最后一个语句是没有意义的,您应该使用的是:
[_images removeObjectAtIndex:button.tag];
[self rearrangeButtons:button.tag];若要向应用程序添加是否正确的检查,请尝试将以下代码添加到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;
}编辑:在循环中编辑的代码,用于打印对象类。
EDIT2:所以我把你的代码放到一个新的项目中,ButtonManager。本质上是可以的,但是你有一些问题。首先,即使不存在文件,也可以对文件名进行索引,这样索引就可能不同步。其次,对button.tag使用%lu格式,但这是一个整数,所以应该使用"%d“。最后,从数组中删除按钮,但不删除图像或缩略图。
如果您下载该项目,您将在所有需要更改的地方看到警告,以使其正常工作。我不知道为什么按钮索引会损坏--也许是其他代码。无论如何,在代码中添加了一个数据一致性测试--在代码中对其进行零星调用--如果它曾经失败过,那么您就知道您的问题在最后一个好的测试和最新的失败测试之间。
该项目在启动时启动一条"deleteItem:“消息,只要您点击”是“,就会继续删除数组中间的项。
发布于 2012-09-08 06:14:13
用以下方法替换您的功能
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"YES"]) {
NSLog(@"YES was selected.");
UIButton *button = _clickedButton;
[self rearrangeItems:button.tag];
}
}
- (void)rearrangeItems:(int)startIndex {
for (UIButton *button in _buttons) {
// Shift the tags down one
if (button.tag > startIndex) {
NSLog(@"called here");
[button removeFromSuperview];//remove the button from the scrollview
[_buttons removeObjectAtIndex:button.tag];//remove the button object from _buttons array
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;
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)];
}
[scrollView setNeedsDisplay];
}
}希望这能有所帮助。
https://stackoverflow.com/questions/12188281
复制相似问题