将图像加载到UIScrollView中

时间:2012-10-10 01:43:18

标签: ios xcode uiscrollview uiimageview

我希望有人可以帮助我,我不知道还能做些什么。我有一个UIScrollView加载一组44个图像,然后允许用户翻阅它们。我的代码正在运行,然后突然停止了。我不知道我改变了什么,所以任何帮助都会非常感激:

上一个视图是一个UITableView,它传递一个字符串以及所选单元格的名称。以下是ScrollView中的ViewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Answer"
                                                                    style:UIBarButtonItemStyleBordered
                                                                   target:self
                                                                   action:@selector(answerPressed:)];

    self.navigationItem.rightBarButtonItem = rightButton;

    if([questionSetName isEqualToString:@"Limitations"]){

        [self limitationsView];
    }
}

这是limitsView:

-(void)limitationsView{

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, self.view.frame.size.height)];

    scroll.pagingEnabled = YES;

    NSArray *unsortedArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"Images/Limitations"];

    NSMutableArray *limitationImages = [[NSMutableArray alloc] init];

    for (int x = 0; x<[unsortedArray count]; x++) {

        NSString *fileName = [[unsortedArray objectAtIndex:x] lastPathComponent];

        [limitationImages insertObject:fileName atIndex:x];
    }

    NSArray *sortedArray = [limitationImages sortedArrayUsingFunction:finderSortWithLocal
                                                              context:(__bridge void *)([NSLocale currentLocale])];

    for(int i = 0; i< [sortedArray count]; i++){

        CGFloat xOrigin = i * self.view.bounds.size.width;

        UIImageView *limitationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];;

        [limitationImageView setImage:[UIImage imageNamed:[sortedArray objectAtIndex:i]]];

        limitationImageView.contentMode = UIViewContentModeScaleAspectFit;

        [scroll addSubview:limitationImageView];

    }

    scroll.contentSize = CGSizeMake(self.view.frame.size.width * [sortedArray count], self.view.frame.size.height);

    [self.view addSubview:scroll];


}

当我到达LimitationsView方法的末尾时,sortedArray包含所有44个图像和正确的名称,如果我尝试设置scrollview的背景颜色,我可以成功地做到这一点。但我的图像没有加载,我不知道为什么?

1 个答案:

答案 0 :(得分:1)

我建议使用

NSLog(@"image:%@",[UIImage imageNamed:[limitationsArray objectAtIndex:i]]); 

查看该点的图像是否为空。如果是这种情况,那么您在该阵列中存储的字符串存在问题,即它们与图像的实际名称不同