-1

我使用故事板和 ARC 模式。这包括三个视图控制器。
每个视图控制器都有自己的类。

VC1 有 Class1.h 和 Class1.m
VC2 有 Class2.h 和 Class2.m。

在每个 m 文件中,我创建图像视图并将图像添加到图像视图中。
然后我将此图像视图添加到滚动视图中。

在这种情况下,我在 class1 中使用了大约 200 张图像后,class2 的图像无法在图像视图中显示。

起初,我认为这是内存问题。
我认为它达到了内存限制,所以它不能再次添加其他图像。

所以,我减小图像大小并重试(从 300Mb 减小到 30Mb)。
但是,它没有用。我不知道。我怎么解决这个问题?请告诉我方法。

那是我的代码。

- (void)viewDidLoad
{

[super loadView];
self.view.backgroundColor = [UIColor grayColor];

UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;

NSInteger numberOfViews = 200;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat xOrigin = i * self.view.frame.size.width;

    // Create a UIImage to hold Info.png
    UIImage *image1 = [UIImage imageNamed:@"Image-001.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"Image-002.jpg"];

    UIImage *image13 = [UIImage imageNamed:@"Image-200.jpg"];

    NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,...,image200,nil];

    UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
    [ImageView setImage:[images objectAtIndex:i]];

    [ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

[self.view addSubview:ScrollView];

}

4

3 回答 3

1
  1. Have a check whether you are adding the imageview to scrollview or to self.view

  2. Check the scroll view frame or scrollview Content size. to make n number of images to UIScrollView u can do this

  3. Declare and add the array object outside for loop so it will consume compiler time.

UIScrollView * scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,self.view.size.width,self.view.size.height)]; [self.view addsubView:scroll];

float xaxis = 10;

for(int i=0;i <20;i++) { UIImageView *imageview=[[UIimageView alloc]initWithFrame:CGRectMake(xaxis,0,150,150)]]; [imageview setImage:[UIImage imagenamed:@""]];//if u want multiple images u can call from array [scroll addSubview:imageview]; xaxis=xaxis*i+50;//apprx }

scroll.contentsize=CGSizemake(xaxis+30,self.view.size.height);

于 2013-02-09T12:37:16.203 回答
0

为每个图像创建单独UIImageView的(具有适当大小)并将所有图像添加UIImageViewUIScrollView并给出适当contentSizeUIScrollView.

获取变量并在.h 文件int x;中声明它并在方法中 分配x=0;viewDidLoad

然后给出适当的 UIImageView 框架

- (void)viewDidLoad
{

[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];

x=0;

UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;

NSInteger numberOfViews = 200;
for (int i = 0; i < numberOfViews; i++) {
    CGFloat xOrigin = i * self.view.frame.size.width;

    // Create a UIImage to hold Info.png
    UIImage *image1 = [UIImage imageNamed:@"Image-001.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"Image-002.jpg"];

    UIImage *image13 = [UIImage imageNamed:@"Image-200.jpg"];

    NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,...,image200,nil];

    UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
    [ImageView setImage:[images objectAtIndex:i]];

    [ScrollView addSubview:ImageView];

    x= x + imageView.frame.size.width;

}
ScrollView.contentSize = CGSizeMake(imageView.frame.size.width * numberOfViews, self.view.frame.size.height);

[self.view addSubview:ScrollView];

}
于 2013-02-09T12:26:58.783 回答
0

为什么要初始化 imageArray 200 次?

为什么for循环中的以下部分::

// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:@"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:@"Image-002.jpg"];

UIImage *image13 = [UIImage imageNamed:@"Image-200.jpg"];

NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,...,image200,nil];

您只需要在 for 循环开始之前初始化图像数组一次,然后遍历这些图像以添加到滚动视图

于 2013-02-09T12:49:25.267 回答