我的UIscrollView
有问题,这就是我所做的:每当用户通过Imagepicker
在相机中选择图像(多个或单个)时,我想在我的Imagepicker
中显示它UIScrollView的。我能够显示它,但是当我再次转到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)];
}
缩略图
- (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];
}
//在我的viewDidLoad
中- (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]);
}
}
修改
{{1}}
答案 0 :(得分:1)
有两点:
createScrollView
移至viewDidLoad
。这些视图应该只创建一次。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];
现在,从viewDidAppear开始,首先更新_thumbs数组(数据源)。然后调用createScrollView函数。在此函数内,首先使用UIScrollView中的标记删除所有子视图。像你一样,再次添加所有的拇指。然后在从createScrollView返回之前调用[self setNeedsDisplay]。