ios:如何使用滚动视图将图像从URL加载到页面控件

dag*_*r24 5 iphone objective-c scrollview uipagecontrol ios

我是iOS的新手.现在我想使用滚动视图将视图图像从URL加载到页面控件.当我运行该程序时,显示第一个图像,但当我尝试滑动到下一个图像或我移动到另一个页面时,程序没有响应.我的猜测可能与图像的大小有关.

以下是我加载图像的代码.我将此代码放入 - (void)viewDidLOad ...

CGRect screenShot = screenView.frame;
screenShot.origin.y = description.frame.origin.y + description.frame.size.height + 30;
screenView.frame = screenShot;

pageControlBeingUsed = NO;

for (int i = 0; i < [myArrayImagesURL count]; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
      UIImageView *iv = [[[UIImageView alloc] initWithFrame:CGRectMake(scrollView.bounds.size.width * i, 0, scrollView.bounds.size.width, scrollView.bounds.size.height)] autorelease];
    iv.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                       [NSURL URLWithString:[myArrayImagesURL objectAtIndex:i]]]];

    [self.scrollView addSubview:iv];
    [iv release];
}

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [myArrayImagesURL count], self.scrollView.frame.size.height);

self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = [myArrayImagesURL count];
Run Code Online (Sandbox Code Playgroud)

Mad*_*dhu 2

看起来您已将 imageView 设置为 autorelease 并释放了它。这应该是错误的。尝试在创建 imageView 时删除 autorelease 标记。现在应该妥善保留。

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(scrollView.bounds.size.width * i, 0, scrollView.bounds.size.width, scrollView.bounds.size.height)];
Run Code Online (Sandbox Code Playgroud)