【问题标题】:Deleting in NSDocumentDirectory and UIScrollView在 NSDocumentDirectory 和 UIScrollView 中删除
【发布时间】:2012-08-30 00:21:06
【问题描述】:

我的代码有问题,可以预览带有UIScrollView 中图像的缩略图。我的图片来自NSDocumentDirectory。我可以从中删除,但是当我从右到左位置开始时,我可以正确地从中删除(就VIEWNSDocumentDirectory 而言)。

问题: 现在,无论如何我都可以删除,但是我有一些问题。

  • 首先,虽然我可以删除,但视图没有排列,我的rearrangeItems: 方法也没有被调用。

  • 第二个,然后第一次加载我可以删除我喜欢的任何东西,但是就像我说的rearrangeItems:方法没有被调用,所以他们的名字 不是renamed

  • 第三,是第一次加载,反正我可以删除,但是当我退出应用程序时,我可以删除但我在NSDocu中的图像没有删除。 p>

希望任何人都可以帮助我。下面是我的代码预览。

- (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)];
            }

        }
    }
}

【问题讨论】:

  • 您有崩溃的堆栈跟踪吗?
  • 崩溃堆栈是什么意思?怎么找?
  • 您的应用程序在调试器中崩溃了吗?然后,您应该能够在 Xcode 中查看崩溃发生的位置,或者在调试器控制台上输入“bt”。
  • 我之前显然试图帮助你,但听起来你现在遇到了一个完全不同的问题。当你说你的“rearrange”方法没有被调用时,你真的是指rearrangeItems:吗?
  • 是的。那个方法就是我的意思。

标签: iphone objective-c ios xcode ipad


【解决方案1】:

您显示“rearrangeButtons”但从不使用它 - 我认为上面的代码已过时。无论如何,这里有一些小问题:

[_images objectAtIndex:button.tag];
[_images removeObjectAtIndex:button.tag];
[_images removeObject:button];

第一个和最后一个语句没有意义,你应该使用的是:

[_images removeObjectAtIndex:button.tag];
[self rearrangeButtons:button.tag];

要仅向应用添加健全性检查,请尝试将此代码添加到重新排列按钮的末尾:

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:”消息,并且只要您点击“是”,就会一直删除数组中间的项目。

【讨论】:

  • 它与日志 UIImageTag 发生崩溃
  • 使用更新的代码。要么你的数组中有一些非 UIView 对象,要么该对象以某种方式被释放。
  • 老板,希望你能帮我解决我在上面想要实现的目标,例如我的图像索引是这样的 0 1 2 3 4,当我删除例如 2 时,它的索引应该是 0 1 2 3,但在我的代码中是这样的 0 1 3 4,所以当我再次删除时,它会崩溃。我真正想要实现的是或者我的想法是再次在我的 NSDocument 中重写图像。谢谢
  • 你好吗,当我删除时,我没有调整。
  • 你读过我给你的一致性测试吗?它只测试了一些东西。如果您无法理解,我担心您将永远无法让您的代码正常工作。
【解决方案2】:

用这些替换你的函数

- (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];
    }
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-10-19
    • 2011-11-30
    • 2023-03-27
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多