ios - 将图像加载到 UIScrollView 中

标签 ios xcode uiscrollview uiimageview

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

前一个 View 是 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];
    }
}

以下是限制 View :

-(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 的背景颜色,我可以成功做到这一点。但我的图像无法加载,我不知道为什么?

最佳答案

我建议使用

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

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

关于ios - 将图像加载到 UIScrollView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810802/

相关文章:

iphone - Storyboard中制作的 UIScrollView 的帧值为零

iphone - 在 Xcode 中使按钮链接到不同的 ViewController

ios - 检测 NSString 中的 URL

ios - CLLocationManager后台运行省电

ios - 测试飞行构建显示无法连接到服务器

swift - 当我之前打开它后返回到 View Controller 时,它不会为标签设置动画 Xcode Swift

ios - 奇怪的事情: Cannot change UIWebView. origin.x在scrollViewDidEndDecelerating

ios - UIAlertController 被窗口上的 View 覆盖

ios - 通过 Pods 将 SocketRocket 添加到 Swift 项目

xcode - 暴露给 Objective-C 代码时 Swift 类的名称不遵循 @objc 重命名