【问题标题】:How can i do memory management for iOS Apps? [closed]如何为 iOS 应用程序进行内存管理? [关闭]
【发布时间】:2013-01-25 03:13:36
【问题描述】:

我使用故事板和 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];

}

【问题讨论】:

  • 放上你的最大代码以便于理解
  • 您的问题不清楚,请尝试改写或添加一些代码
  • @iPatel,抱歉我的问题不完整。我在将图像添加到图像视图时遇到问题。我认为这是内存问题。但是,我不知道我该如何解决。
  • @Dhruv Goei,抱歉我的问题不完整。我在将图像添加到图像视图时遇到问题。我认为这是内存问题。但是,我不知道我该如何解决。
  • wt 在输出中显示???重叠图像???

标签: ios xcode cocoa-touch memory


【解决方案1】:

你为什么要初始化 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 循环开始之前初始化 images 数组一次,然后遍历这些图像以添加到滚动视图中

【讨论】:

    【解决方案2】:
    1. 检查一下您是将 imageview 添加到 scrollview 还是 self.view 中

    2. 检查滚动视图框架或滚动视图内容大小。要为 UIScrollView 制作 n 个图像,你可以这样做

    3. 在 for 循环外声明并添加数组对象,这样会消耗编译时间。

    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

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

    【讨论】:

      【解决方案3】:

      为每个图像创建单独的UIImageView(具有适当的大小),并将所有UIImageView 添加到UIScrollView 并给出contentSize 的正确UIScrollView

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

      然后给出适当的 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];
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-01-25
        • 1970-01-01
        • 2018-11-04
        • 2012-10-15
        • 2018-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-15
        相关资源
        最近更新 更多