更改scrollview的图像或替换图像数组

时间:2013-11-05 08:20:08

标签: ios objective-c uiscrollview

我在滚动视图中更改图像时出现问题,滚动视图问题是,取决于使我必须更改包含其他图像的操作,因为我不更改图片或图像数组,我需要帮助,谢谢

- (void)viewDidLoad
{

    [super viewDidLoad];


    NSMutableArray *imgNames = [[NSMutableArray alloc] initWithObjects:@"cooper.png", @"cabrio.png",@"country.png", nil];


   NSMutableArray *imgArray = [[NSMutableArray alloc] init];
    UIImageView *tempImageView;

    for(NSString *name in imgNames) {

        tempImageView = [[UIImageView alloc] init];
        tempImageView.contentMode = UIViewContentModeScaleAspectFit;
        tempImageView.image = [UIImage imageNamed:name];
        [imgArray addObject:tempImageView];   
    }

    CGSize pageSize = scrollView.frame.size;

    NSUInteger page = 0;

    for(UIView *view in imgArray) {

        [scrollView addSubview:view];

        view.frame = CGRectMake(pageSize.width * page++ + 10, 0, pageSize.width - 20, pageSize.height);
    }

    scrollView.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);

}

- (IBAction)btnAction:(id)sender {


    NSMutableArray *imgNames = [[NSMutableArray alloc] initWithObjects:@"hello.png", @"bye.png",@"trues.png", nil];


   NSMutableArray *imgArray = [[NSMutableArray alloc] init];
    UIImageView *tempImageView;

    for(NSString *name in imgNames) {

        tempImageView = [[UIImageView alloc] init];
        tempImageView.contentMode = UIViewContentModeScaleAspectFit;
        tempImageView.image = [UIImage imageNamed:name];
        [imgArray addObject:tempImageView];   
    }

    CGSize pageSize = scrollView.frame.size;

    NSUInteger page = 0;

    for(UIView *view in imgArray) {

        [scrollView addSubview:view];

        view.frame = CGRectMake(pageSize.width * page++ + 10, 0, pageSize.width - 20, pageSize.height);
    }

    scrollView.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);

}
}

1 个答案:

答案 0 :(得分:0)

如果已在viewDidLoad中分配了UIImageView,则可以直接设置图像,

for(UIImageView *view in imgArray) {

     view.image = [UIImage imageNamed:[imgNames objectAtIndex:[imgArray indexOfObject:view]]]
}