gpt4 book ai didi

iphone - 自定义图像选择器/UIScrollView 未出现在 View 中

转载 作者:行者123 更新时间:2023-11-28 20:34:28 25 4
gpt4 key购买 nike

这里是新开发人员,我正在使用 ray wenderlich 的自定义图像选择器。但我想做的是显示选择器,而不是通过按下按钮,而是在加载 View 时。我似乎无法让它出现。这是我所做的,我不知道哪里出了问题。感谢您的帮助。

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

- (void) createScrollView {
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,200.0f)];

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*140+24, row*150+10, 100, 100);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:view];
[view addSubview:button];

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

}

[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
}
- (IBAction)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
}

- (void)viewDidLoad
{
[self createScrollView];
_images = [[NSMutableArray alloc] init];
_thumbs = [[NSMutableArray alloc] init];

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]){
[_images addObject:[UIImage imageWithContentsOfFile:savedImagePath]];

NSLog(@"file exists");
}
NSLog(@"file does not exist");
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

最佳答案

首先在 scrollview 中添加按钮,最后在 for 循环之后在 self.view 中添加 scrolview。

- (void) createScrollView {
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,300.0f,200.0f)];

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*140+24, row*150+10, 100, 100);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;

[view addSubview:button];

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

}

[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];

[self.view addSubview:view];
}

我希望它对你有用。

关于iphone - 自定义图像选择器/UIScrollView 未出现在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11077368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com