3

でプレビューを作成して、選択ViewControllerしたすべての画像がimagePickerscrollView に表示されるようにします。はい、 でプレビューを作成できましthumbnailviewDidAppear。も再追加するため、画像の数が再び追加され、画像が重なるscrollViewためビューで削除するのが難しくなります。私が必要としていたscrollviewのは、ビューが表示されるたびに、新しい画像を追加するときに更新することです。

これは、私が長い間問題を抱えているコードのプレビューです。

- (id) initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        _images =  [[NSMutableArray alloc] init];
        _thumbs =  [[NSMutableArray alloc] init];
    }
    return self;
}
- (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)];
}

- (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:@"Images%d.png", i]]; 
        if([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]){ 
            [self addImage:[UIImage imageWithContentsOfFile:savedImagePath]]; 
        } 
    }
}

助けていただければ幸いです。

そして、これを使用すると、削除してから追加するのに大いに役立ちます。または、これはすべてのサブビューを完全に削除/削除してからREadd する方法ですか? 助けてくれてありがとう。

そして、これは役に立ちますか?ありがとうございました

for(UIView *subview in [scrollView subviews]) {
    if([subview isKindOfClass:[UIView class]]) {
        [subview removeFromSuperview];
    } else {

    }
}    

消去:

- (void)deleteItem:(id)sender {
        _clickedButton = (UIButton *)sender;
        UIAlertView *saveMessage = [[UIAlertView alloc] initWithTitle:@""
                                                              message:@"DELETE?"
                                                             delegate:self
                                                    cancelButtonTitle:@"NO"
                                                    otherButtonTitles:@"YES", nil];
}
- (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];
            [_images objectAtIndex:button.tag];
            [_images removeObjectAtIndex:button.tag];
            [_images removeObject:button];

            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");
    }
}
4

1 に答える 1

7

これが実際の例です:Googleコード

私はコードを書きました、そして私はこれを持っています。ビューを幅 5 に揃えますが、高さは必要ですが、スクロールビューの高さが変わります。

ボタンのリストを含む新しいNSMutableArray名前を作成する必要があります。_buttons

- (void)addImage:(UIImage *)imageToAdd {
    [_images addObject:imageToAdd];
    [_thumbs addObject:[imageToAdd imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]];

    int row = floor(([views count] - 1) / 5);
    int column = (([views 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 = [views 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 {
    UIButton *button = (UIButton *)sender;
    [button removeFromSuperview];
    [views removeObjectAtIndex:button.tag];
    [_buttons removeObjectAtIndex:button.tag];

    [self rearrangeButtons:button.tag];
}

- (void)rearrangeButtons:(int)fromTag {
    for (UIButton *button in _buttons) {
        // Shift the tags down one
        if (button.tag > fromTag) {
            button.tag -= 1;
        }
        // Recalculate Position
        int row = floor(button.tag / 5);
        int column = (button.tag - (row * 5));
        // Move
        button.frame = CGRectMake(column*60+10, row*60+10, 60, 60);
        if (button.tag == [_buttons count] - 1) {
            [scrollView setContentSize:CGSizeMake(300, row*60+20+60)];
        }
    }
}

注: このrearrangeButtonsメソッドでは、変更をアニメーション化することができます。

ファイルを並べ替えるコードは次のとおりです。

- (void)rearrangeButtons:(int)fromTag {
    for (UIButton *button in _buttons) {
        // Shift the tags down one
        if (button.tag > fromTag) {
            // Create name string
            NSString *imageName = [NSString stringWithFormat:@"images%i.png", button.tag];
            // Load image
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentFile = [paths objectAtIndex:0];
            NSSting *oldFilePath = [documentFile stringByAppendingPathComponent:imageName];
            NSData *data = [[NSData alloc] initWithContentsOfFile:oldFilePath];
            button.tag -= 1;
            // Save the image with the new tag/name
            NSString *newImageName = [NSString stringWithFormat:@"images%i.png", button.tag];
            NSString *newFilePath = [documentFile stringByAppendingPathComponent:newImageName];
            [data writeToFile:newFilePath atomically:YES];
            // Delete the old one
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSError *err = nil;
            if (![fileManager removeItemAtPath:file error:&err]) {
                // Error deleting file
            }
        }
        // Recalculate Position
        int row = floor(button.tag / 5);
        int column = (button.tag - (row * 5));
        // Move
        button.frame = CGRectMake(column*60+10, row*60+10, 60, 60);
        if (button.tag == [_buttons count] - 1) {
            [scrollView setContentSize:CGSizeMake(300, row*60+20+60)];
        }
    }
}
于 2012-07-16T15:53:08.323 に答える