1

我正在尝试创建一个非常基本的图像库,其中包含三个图像,用户将能够滑过并查看图像。

我的解决方案正在使用UIColor

NSArray *colors = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor redColor], [UIColor greenColor], nil];
for (int i = 0; i < colors.count; i++) {
    CGRect frame;
    frame.origin.x = 320 * i;
    frame.origin.y = 0;
    frame.size = CGSizeMake(320, 208);

    UIView *subview = [[UIView alloc] initWithFrame:frame];
    subview.backgroundColor = [colors objectAtIndex:i];
    [self.slideshow addSubview:subview];
}

self.slideshow.contentSize = CGSizeMake(320 * [colors count], 208);

但图像不显示类似的解决方案

NSArray *images = [NSArray arrayWithObjects:[UIImage imageWithContentsOfFile:@"pic_1_3a.jpg"], [UIImage imageWithContentsOfFile:@"pic_1_3b.jpg"], [UIImage imageWithContentsOfFile:@"pic_1_3c.jpg"], nil];
for (int i = 0; i < images.count; i++) {
    CGRect frame;
    frame.origin.x = 320 * i;
    frame.origin.y = 0;
    frame.size = CGSizeMake(320, 208);

    UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
    subview.image = [images objectAtIndex:i];
    [self.slideshow addSubview:subview];
}

self.slideshow.contentSize = CGSizeMake(320 * [images count], 208);

任何想法?UIScrollView存在UIView于.

在此处输入图像描述

4

2 回答 2

2

尝试并改变

NSArray *images = [NSArray arrayWithObjects:[UIImage imageWithContentsOfFile:@"pic_1_3a.jpg"], [UIImage imageWithContentsOfFile:@"pic_1_3b.jpg"], [UIImage imageWithContentsOfFile:@"pic_1_3c.jpg"], nil];

NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"pic_1_3a.jpg"], [UIImage imageNamed:@"pic_1_3b.jpg"], [UIImage imageNamed:@"pic_1_3c.jpg"], nil];
于 2013-06-11T08:25:26.553 回答
0
- (void)viewDidLoad
 {
 arrImages=[[NSArray alloc]initWithObjects: @"bigbazaar_card.jpg", @"CouponImage.png", @"futureBazaar_card.jpg",@"No_deal.png",@"Reliance_card.jpg",@"wallmart_card.jpg",@"westside_card.jpg", nil];
int x=5;
int y=5;
for (int i=0; i<[arrImages count]; i++) {
    NSString *str=[arrImages objectAtIndex:i];
    UIImageView *currentimage=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 150, 220)];
    [currentimage setImage:[UIImage imageNamed:str]];
    [scroll addSubview:currentimage];

    UIButton *btnTag=[[UIButton alloc]initWithFrame:CGRectMake(x, y, 150, 220)];
    [btnTag setTitle:@"" forState:UIControlStateNormal];
    selectedImageIndex=i;
    btnTag.tag=i;
    NSLog(@"btntag-->%i",btnTag.tag);

    [btnTag  addTarget:self action:@selector(BtnShowImage:) forControlEvents:UIControlEventTouchUpInside];
    [scroll addSubview:btnTag];
    x= x+170;
}    
scroll.contentSize = CGSizeMake(170* arrImages.count,scroll.frame.size.height);
}

并且您必须在 .h 文件中声明 UIScrollView 的委托,如下所示:

@interface ViewController : UIViewController<UIScrollViewDelegate>

and you have to implement ScrollView's Delegate method if you want to display image on click as shown below:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  CGPoint contentOffset = scrollView.contentOffset;
contentOffset.x += scrollView.frame.size.width / 2;
NSInteger index = contentOffset.x / scrollView.frame.size.width;
NSString *strTemp=[arrImages objectAtIndex:index];
[imageData setImage:[UIImage imageNamed:strTemp]];
}
于 2013-06-11T09:14:32.330 回答