我在将图像从文件加载到数组中时遇到一些问题。我使用了我在这里找到的问题的组合,但我没有想法......我对 Objective-C 很陌生,对其余的都生疏了。
我的 viewDidLoad 只是调用我的 showPics 方法,并且为了测试目的,我让 _imgView 仅显示数组中位置 1 处的图像。
这也很可能是我展示图像的方式的问题。我的 Storyboard 中有一个 ViewController 和一个 ImageView(标题为:imageView)。
这是我的 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
如果您还需要任何其他信息,请告诉我,并提前致谢!
In your 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];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)