2

私のコードに問題があり、の画像を含むサムネイル付きのプレビューを作成することができましたUIScrollView。私の画像はからNSDocumentDirectoryです。私はそれから削除することはできますが、右から左の位置から始めると、 ( VIEW&の観点から)正しく削除することができます。NSDocumentDirectory

問題: とにかく削除できますが、いくつか問題があります。

  • まず、削除することはできますが、ビューが配置rearrangeItems:されておらず、メソッドも呼び出されていません。

  • 第二に、最初のロードでは、とにかく好きなように削除できますが、私が言ったように、rearrangeItems:メソッドが呼び出されていないので、それらの名前はありませんrenamed

  • 3つ目は、最初の読み込みで、とにかく削除できますが、アプリを終了すると削除できますが、の画像は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)];
            }

        }
    }
}
4

2 に答える 2

2

「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-08-30T11:46:28.837 に答える
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];
    }
}

お役に立てれば。

于 2012-09-08T06:14:13.207 に答える