uiscrollview 懒加载

Uiscrollview lazy loading(uiscrollview 懒加载)
本文介绍了uiscrollview 懒加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我使用 AssetsLibrary 获得了所有 iphone 图像.我在 imageview 中传递 UIImage 对象并在滚动视图中显示图像.iphone 中有 200 多张图像,我必须在不使用分页的情况下垂直显示滚动视图中的所有图像.这需要大量时间来显示图像,并且还存在内存问题.是否有任何代码可以在滚动视图中延迟加载 iphone 图像

I have got all images of iphone using AssetsLibrary.I am passing UIImage object in imageview and display images in scrollview. There are more than 200 images in iphone and i have to display all images in scrollview vertically without use of paging.This takes lots of time to display images and it has also memory issues. Is there any code for lazy loading of iphone images in scrollview

推荐答案

我最近一直在研究这个问题,并查看了网络上的大量示例代码.没有一个真正做到延迟加载(尽管声称)或具有比我愿意忍受的更复杂(不必要的花里胡哨).PhotoPicker 的 Apple WWDC 视频似乎显示延迟加载,但似乎更侧重于预切片图像的平铺,因此没有太大帮助.

I've been working on this recently and have checked out loads of example code on the web. None of it really did lazy loading (despite the claim) or had much more complexity (needless bells and whistles) than I was willing to put up with. The Apple WWDC videos for PhotoPicker appear to show lazy loading but seem more focused on tiling of pre-sliced up images, so not much help there.

我最终做的是一次加载所有 aspectThumbnails,因为它们很小并且不占用那么多内存,然后加载 fullScreenImage scrollViewDidEndDecelerating 按需表示并重新加载 aspectThumbnail 以使 imageView 离开屏幕.效果非常流畅和视觉上,图像有点粗糙,然后很快(通过背景加载)被更高分辨率的图像替换.

What I've ended up doing is to load all the aspectThumbnails at once, since they are small and don't take up that much memory and then load the fullScreenImage representation on demand from scrollViewDidEndDeceleratingand reload the aspectThumbnail for the imageView going off-screen. The effect is very fluid and visually, the image comes in a little rough and then quickly (through background loading) is replaced with the higher resolution image.

它可能会使用更多的细化 - 可能会为 currentPage +1 加载全分辨率图像 &-1.我还没有解决这个问题.此外,我不完全确定我对块的使用是否处于最佳状态——但没有出错.

It can probably use more refinement - perhaps loading full resolution images for the currentPage +1 & -1. I haven't got around to that as yet. Also, I'm not entirely sure if my use of blocks in optimal – but not getting an errors.

我从 Segue 调用它并在我的根 viewController 上的 prepareForSegue: 方法中设置 assetsArray(ALAssets 的 NSArray).

I call this from a Segue and set the assetsArray (NSArray of ALAssets) from within the prepareForSegue: method on my root viewController.

//  ScrollViewController.h
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface ScrollViewController : UIViewController <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, atomic) ALAssetsLibrary* assetLibrary;
@property (nonatomic,strong)NSMutableArray* assetsArray;
@end

//ScrollViewController.m

//ScrollViewController.m

#import "ScrollViewController.h"

@interface ScrollViewController ()

@property (nonatomic,assign) CGSize currentImageSize;
@property (nonatomic, assign)int currentPages;
@property (nonatomic, assign)int currentPageNum;
@property (nonatomic, strong)UIImage* placeHolder;
@end



@implementation ScrollViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    //self.placeHolder = [UIImage imageNamed:@"loader.jpg"];
    self.scrollView.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.assetsArray != nil) {
        self.currentPages = [self.assetsArray count];

        CGSize size = self.scrollView.frame.size;
        int num = self.currentPages;

        self.scrollView.contentSize=CGSizeMake(size.width*num, size.height);

        [self loadThumbnails];

        self.currentPageNum = 0;
        [self loadFullScreenImageByIndex:self.currentPageNum];
    }
}

-(void)loadThumbnails
{
    int pageCount = self.currentPages;
    CGSize size = self.scrollView.frame.size;
    self.scrollView.contentSize=CGSizeMake(size.width*pageCount, size.height);

    for (int i = 0; i < pageCount; i++) {

        ALAsset *asset = [self.assetsArray objectAtIndex:i];//
        CGRect imageViewFrame;

        // x offset is determined by arrayIndex
        imageViewFrame.origin.x = self.scrollView.frame.size.width * i;
        imageViewFrame.origin.y = 0;
        imageViewFrame.size = self.scrollView.frame.size;

        self.currentImageSize = imageViewFrame.size; // THIS IS WRONG

        UIImage *image = [[UIImage alloc] initWithCGImage:asset.aspectRatioThumbnail];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.frame = imageViewFrame;
        imageView.tag = i+1;// start tags at 1

        [self.scrollView addSubview:imageView];
    }
}


- (void)viewDidUnload {
    [self setScrollView:nil];
    [super viewDidUnload];
}

- (void)loadFullScreenImageByIndex:(int)index
{
    int arrayIndex = index;
    int tagNumber = index+1;
    ALAsset *asset = [self.assetsArray objectAtIndex:arrayIndex];

    __weak typeof(self) weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        UIImage *tmpImage = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];

        __strong __typeof__(weakSelf) strongSelf = weakSelf;
        if ([strongSelf.scrollView viewWithTag:tagNumber] != nil){

            dispatch_async(dispatch_get_main_queue(), ^{
                __strong __typeof__(weakSelf) strongSelf = weakSelf;
                if ([strongSelf.scrollView viewWithTag:tagNumber]!= nil){
                    UIImageView * tmpImageView = (UIImageView*)[strongSelf.scrollView viewWithTag:tagNumber];
                    tmpImageView.image = tmpImage;
                }
            });
        }
    });
}

- (void)loadThumbnailImageByIndex:(int)index
{
    int arrayIndex = index;
    int tagNumber = index+1;

    ALAsset *asset = [self.assetsArray objectAtIndex:arrayIndex];//

    __weak typeof(self) weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        UIImage *tmpImage = [[UIImage alloc] initWithCGImage:asset.aspectRatioThumbnail];        
        __strong __typeof__(weakSelf) strongSelf = weakSelf;
        if ([strongSelf.scrollView viewWithTag:tagNumber] != nil){

            dispatch_async(dispatch_get_main_queue(), ^{
                __strong __typeof__(weakSelf) strongSelf = weakSelf;
                if ([strongSelf.scrollView viewWithTag:tagNumber]!= nil){
                    UIImageView * tmpImageView = (UIImageView*)[strongSelf.scrollView viewWithTag:tagNumber];
                    tmpImageView.image = tmpImage;
                }
            });
        }
    });
}

- (void)manageImages
{
    int currentPage = (self.scrollView.contentOffset.x / self.currentImageSize.width);

    if (currentPage != self.currentPageNum){
        [self loadThumbnailImageByIndex:self.currentPageNum]; //pg no longer visible so load thumb
        [self loadFullScreenImageByIndex:currentPage]; // load full
        self.currentPageNum = currentPage;// store
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self manageImages];
}

这篇关于uiscrollview 懒加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中设置滚动视图内容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自动滚动)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延迟加载)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模拟器上运行时,使用 iOS 6.0 SDK 并为 iOS 5 Target 构建会导致 UIScrollView setMinimumZoomScale 失败) -
Create partial-screen UIPageViewController programmatically(以编程方式创建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可缩放?)