从图像文件夹加载数组 - xcode

时间:2012-10-22 15:53:59

标签: ios xcode arrays image

我在将文件中的图像加载到数组时遇到了一些问题。我已经使用了我在这里找到的一系列问题,而且我没有想法......我是对Objective-c的新手并且在其余部分生锈了。

我的viewDidLoad只是调用我的showPics方法,为了测试,我让_imgView只显示数组中位置1的图像。

这也很可能是我展示图像的方式的问题。我的Storyboard中有一个ViewController和一个ImageView(标题为:imgView)。

这是我的showPics方法:

-(void)showPics
{
    NSArray *PhotoArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"Otter_Images"];
    NSMutableArray *imgQueue = [[NSMutableArray alloc] initWithCapacity:PhotoArray.count];
    for (NSString* path in PhotoArray)
    {
        [imgQueue addObject:[UIImage imageWithContentsOfFile:path]];
    }
    UIImage *currentPic = _imgView.image;
    int i = -1;

    if (currentPic != nil && [PhotoArray containsObject:currentPic]) {
        i = [PhotoArray indexOfObject:currentPic];
    }

    i++;
    if(i < PhotoArray.count)
        _imgView.image= [PhotoArray objectAtIndex:1];

}

这是我的viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showPics];
}

这是我的ViewController.h

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@end

如果您还有其他需要,请告诉我,并提前感谢您!

1 个答案:

答案 0 :(得分:3)

showPics方法中,除了最初的'for-loop'之外,您对PhotoArray的所有引用都应该是对imgQueue的引用。 PhotoArray是路径名列表。 imgQueue是实际UIImage个对象的数组。

-(void)showPics {
    NSArray *PhotoArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"Otter_Images"];
    NSMutableArray *imgQueue = [[NSMutableArray alloc] initWithCapacity:PhotoArray.count];
    for (NSString* path in PhotoArray) {
        [imgQueue addObject:[UIImage imageWithContentsOfFile:path]];
    }

    UIImage *currentPic = _imgView.image;
    int i = -1;

    if (currentPic != nil && [imgQueue containsObject:currentPic]) {
        i = [imgQueue indexOfObject:currentPic];
    }

    i++;
    if(i < imgQueue.count) {
        _imgView.image = [imgQueue objectAtIndex:1];
    }
}
相关问题