【问题标题】:crash when loading big sizes images on scroll view iPad在滚动视图 iPad 上加载大尺寸图像时崩溃
【发布时间】:2014-07-11 19:25:02
【问题描述】:

我有 31 张尺寸为 1448 *2048 的图片。我必须将它添加到滚动视图并在 iPad 上水平交换。但问题是当我交换第 6 张图像时。它因内存警告而崩溃。

我使用了 developer.apple.com 的页面控制示例的逻辑。 在此类的 ViewDidload 中,我将滚动视图框架设置为与页面控件示例相同。

我的页面加载代码函数是

- (void)loadPage:(int)page 
{

 if (page < 0) return;
    if (page >= [_imgArray count]) return;

    // replace the placeholder if necessary aViewController is NSMutable Array.
    ImageViewC *controller = [aViewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null]) {

        controller = [[ImageViewC alloc] initWithImage:[_imgArray objectAtIndex:page]];
  //controller.screen = currentPage;
  [aViewControllers replaceObjectAtIndex:page withObject:controller];
        [controller release];
    }

    // add the controller's view to the scroll view
    if (nil == controller.view.superview)
 {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
    }
}

滚动功能是

- (void)scrollViewDidScroll:(UIScrollView *)sender
{

    // Switch the indicator when more than 50% of the previous/next page is visible
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    self.currentPage = floor(scrollView.contentOffset.x / pageWidth) + 1;
    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    [self loadPage:page - 1];
    [self loadPage:page];
    [self loadPage:page + 1];

 if (page == -1) 
  return;
}

谁能帮帮我。请修改代码如何管理内存,以便我能够在此滚动上交换 31 个图像。

【问题讨论】:

    标签: iphone


    【解决方案1】:

    一张图片占用超过 11 MB 的 RAM,因此您应该不会对应用崩溃感到惊讶。

    您需要释放不可见的图像,并且您也应该对图像进行分区。 loadPage: 方法看起来过于复杂,看不懂它在做什么,也不知道 ImageViewC 是什么。

    如果您是 Apple 的注册开发人员,请查看 WWDC 视频,其中一个是关于 UIScrollViews 的,它展示了如何加载许多图像并正确处理。

    【讨论】:

    • 有问题的 WWDC 示例是 2010 年的“photoscroller”;代码和视频一样可用。两者都很棒!
    • 谢谢。在此代码中,imageViewC 是控制器类,其中有 imageView。你可以发送 uiscrollviews 的 wwdc 视频链接吗?
    • 登录后可从 Apple 网站获取视频 - 它将带您到 iTunes。
    【解决方案2】:

    您应该卸载当前不可见的图像。这应该有助于减少内存占用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      相关资源
      最近更新 更多