これは私UIscrollView
がやったことです:ユーザーがカメラロールで画像(複数または単一)を選択するたびImagepicker
に、UIScrollViewに表示したいと思います。表示はできたのですが、再度 に行ってImagepicker
再度画像を選択するとが更新されずUIScrollView
、 を入れてみ[self createScrollView]
ましたviewDidAppear
が を再作成してUIScrollView
も更新されず、古い画像と新しい画像が結合されてしまいます. だから私はそれらを入れましたが、別の場所に行ってからもう一度戻ったviewDidLoad
ときにのみ更新されます。View Controller
//UIScrollView
サムネイル画像付き の私の
- (void) createScrollView {
self.slotBg = [[UIView alloc] initWithFrame:CGRectMake(43, 370, 300, 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,134.0f)];
[slotBg addSubview:self.scrollView];
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, 60);
[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(330, (row+1) * 60 + 10)];
}
// 私のビューでDidLoad
- (void)viewDidLoad
{
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]);
[self createScrollView];
}
編集:
- (void) viewDidLoad {
[self createScrollView];
[_thumbs removeAllObjects];
UIView *removeView;
for(int i = 0; i < _thumbs.count; ++i) {
while((removeView = [scrollView viewWithTag:i]) != nil) {
[removeView removeFromSuperview];
}
{
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]);
}
}