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

标签 iphone ios xcode

这里是新开发人员,我正在使用 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/

相关文章:

ios - 我怎样才能使应用程序图标像 iOS 内置的时钟和日历图标一样响应

iOS - 简单的 SSL 套接字连接

xml - SWXMLHash 迭代错误

ios - 无法将 Cordova/Ionic App 的二进制文件提交到 Appstore

iphone - 如何使用 iPhone SDK 获取网络上的计算机或设备列表及其 IP 地址和名称?

iphone - 应用程序已准备出售,但 iAd "receiving test ads"!

iphone - 无法复制 App Sandbox 中的项目

ios - Swift 导航栏搜索栏在单击时忽略搜索文本

ios - 自拍模式下的图像选择器

iphone - 当 UINavigationBar 隐藏/显示时如何防止 View 调整大小/转换