从图像文件夹加载数组 – xcode
我有一些麻烦加载文件中的图像到数组中。 我已经使用了我在这里find的一些问题的组合,并没有想法….我是新的objective-c和其余的生锈。
我的viewDidLoad只是简单地调用我的showPics方法,并为了testing,我有_imgView只显示数组中的位置1的图像。
这也可能是我展示图片的一个问题。 我的故事板中有一个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
请让我知道,如果你需要别的,并提前谢谢你!
在你的showPics
方法中,除了最初的“for-loop”之外,所有对PhotoArray
的引用PhotoArray
应该是对imgQueue
引用。 PhotoArray
是path名列表。 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]; } }