0

scrollView で画像を削除するのに問題があります。UIScrollView に画像を配置してボタンを追加すると、クリックすると削除するかどうかを尋ねる警告ビューが表示されます。NSDocumentDirectoryはいの場合、ビューではなく自分で削除します。私の画像はからNSDocumentDirectory選んだものImagePickerです。

- (void)addImage:(UIImage *)image {
    [_images addObject:image];
    [_thumbs addObject:[image imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]];
    [self createScrollView];
}

- (void) createScrollView {

    [scrollView setNeedsDisplay];
    int row = 0;
    int column = 0;
    for(int i = 0; i < _thumbs.count; ++i) {

        UIImage *thumb = [_thumbs objectAtIndex:i];
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(column*60+10, row*60+10, 60, 75);
        [button setImage:thumb forState:UIControlStateNormal];
        [button addTarget:self 
                   action:@selector(buttonClicked:) 
         forControlEvents:UIControlEventTouchUpInside];
        button.tag = i; 

        [scrollView addSubview:button];

        if (column == 4) {
            column = 0;
            row++;
        } else {
            column++;
        }

    }
   [scrollView setContentSize:CGSizeMake(300, (row+1) * 60 + 10)];
}

- (IBAction)buttonClicked:(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"]) {
            UIButton *button = _clickedButton;
            [button removeFromSuperview];
            [_images objectAtIndex:button.tag];
            [_images removeObjectAtIndex:button.tag];

            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"images%lu.png", button.tag]];
            [fileManager removeItemAtPath: fullPath error:NULL];
            NSLog(@"image removed");
}

- (void)viewDidLoad
{
        self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43, 370, 310, 143)];
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = self.slotBg.bounds;
        gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
        [self.slotBg.layer insertSublayer:gradient atIndex:0];
        [self.view addSubview:self.slotBg];
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,130.0f)];
        [slotBg addSubview:self.scrollView];
}

- (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:@"oneSlotImages%d.png", i]]; 
        NSLog(@"savedImagePath=%@",savedImagePath);
        if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
            [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 
            NSLog(@"file exists");
        } 
    } 
    NSLog(@"Count : %d", [_images count]);
}
4

4 に答える 4

1

createScrollView配列に画像を追加するたびに呼び出しています。やっているように見えるのは、同じ画像を重ね合わせたボタンを作成していることです。あなたのコードはそれをビューから削除しているようですが、そのすぐ下に同様のボタンがあるようです。したがってcreateScrollView、すべての画像を追加してから削除した後にのみ呼び出してくださいaddImage:

for(int i = 0; i <= 100; i++) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"oneSlotImages%d.png", i]]; 
    NSLog(@"savedImagePath=%@",savedImagePath);
    if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
        [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 
        NSLog(@"file exists");
    } 
}
[self createScrollView];
于 2012-06-25T07:10:25.473 に答える
0

タグの値をbuttonに設定します。次に、次の関数でボタンを取得します。

    (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
         UIButton *btn = (UIButton*)[scrollview viewWithTag:button.tag];
         //your button tag
         [btn removeFromSuperView];
}

于 2012-06-25T06:57:35.803 に答える
0
//after deleting the image from document directory just call viewDidAppear again...

[self viewDidAppear:yes];

これがあなたを助けることを願っています...

于 2012-06-25T07:30:05.923 に答える