iphone - 在 ScrollView iPad 上加载大尺寸图像时崩溃

标签 iphone

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

我使用了来自developer.apple.com的页面控制示例的逻辑。 在此类的 ViewDidload 中,我设置了与页面控件示例相同的 ScrollView 框架。

我的页面加载代码功能是

- (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 个图像。

最佳答案

一张图像占用超过 11 MB 的 RAM,因此您看到应用崩溃时不应感到惊讶。

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

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

关于iphone - 在 ScrollView iPad 上加载大尺寸图像时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3642082/

相关文章:

iphone - 为什么苹果这样做: MKMapRectIntersection(boundingMapRect, worldRect)?

iphone - 创建自定义动画属性

iphone - 在 TextField 中显示日期

iphone - 遇到分配/释放问题

iphone - 带有 TabBar 的基于导航的应用程序

iphone - iPhone 的 Thrift 框架

iphone - 正则表达式模式和/或 NSRegularExpression 在非常大的文件上搜索有点太慢了,可以优化吗?

iphone - Xcode 4.5.1 iOS5/5.1 IBAction 不工作。在 iOS6 中很好

javascript - iPhone 方向 HTML 隐藏

iphone - Cocoa Touch 中的音调生成