ios - 使用页面控制的 UIScrollView 中的横向模式下图像无法正确加载

标签 ios objective-c memory-management uiscrollview uipagecontrol

我需要能够在横向和纵向模式下以及所有设备上正确加载图像。 我还需要在加载图像时进行适当的内存管理。现在我的代码一次加载所有图像,但将来我将添加更多图像,因此需要优化的内存解决方案。

- (void)viewDidLoad {
[super viewDidLoad];

self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationController.navigationBarHidden = YES;

_photoArray = [NSArray arrayWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg",@"image4.jpg",@"image5.jpg",@"image6.jpg",@"image7.jpg",@"image8.jpg",@"image9.jpg", nil];

self.pageControl.numberOfPages = _photoArray.count;

CGSize screen = [[UIScreen mainScreen] bounds].size;

for(int i = 0; i < _photoArray.count; i++)
{
    self.subScrollView = [[UIScrollView alloc] init];
    self.subScrollView.frame = CGRectMake((i * screen.width), 0, screen.width, screen.height);
    [self.subScrollView setMinimumZoomScale:1];
    [self.subScrollView setZoomScale:1];
    [self.subScrollView setMaximumZoomScale:3];
    self.subScrollView.delegate = self;


    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_photoArray[i]]];
    imgView.frame = self.subScrollView.bounds;
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.clipsToBounds = YES;
    imgView.tag = i;

    [self.subScrollView addSubview:imgView];

    [_scrView addSubview:self.subScrollView];

    if(i == _photoArray.count-1)
        _scrView.contentSize = CGSizeMake(self.subScrollView.frame.origin.x + self.subScrollView.frame.size.width, screen.height);
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView viewWithTag:self.pageControl.currentPage];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//page control
CGFloat pageWidth = self.scrView.frame.size.width;
self.pageControl.currentPage = (self.scrView.contentOffset.x +pageWidth/2)/pageWidth;
}

最佳答案

我认为优化内存的最佳选择是使用 UICollectionView 而不是 UIScrollView。您还可以将 UIPageControl 与 UICollectionView 一起使用。

    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return 1;
    }

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return self.imagesArray.count;
    }

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewCell *cell == [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",indexPath.row]]];
        [cell.addSubView:image];
        return cell;
    }

关于ios - 使用页面控制的 UIScrollView 中的横向模式下图像无法正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29508386/

相关文章:

iphone - TableView 不会显示提供的数据

iphone - 从控制台构建时找不到头文件

iphone - Tableviecells 没有被释放......内存管理问题

c - 是否可以按位分配动态内存?

ios - Monotouch SIGSEGV 仅在模拟器中

ios - 访问用户联系人电话号码 CNContacts

ios - 从 UIImageView 创建 UIImage

objective-c - 如何观察 NSTextStorage 的变化

iphone - 如何让我的应用程序支持广告?

java - Date对象的内存使用情况