久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

uiscrollview 懶加載

Uiscrollview lazy loading(uiscrollview 懶加載)
本文介紹了uiscrollview 懶加載的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我使用 AssetsLibrary 獲得了所有 iphone 圖像.我在 imageview 中傳遞 UIImage 對(duì)象并在滾動(dòng)視圖中顯示圖像.iphone 中有 200 多張圖像,我必須在不使用分頁(yè)的情況下垂直顯示滾動(dòng)視圖中的所有圖像.這需要大量時(shí)間來(lái)顯示圖像,并且還存在內(nèi)存問(wèn)題.是否有任何代碼可以在滾動(dòng)視圖中延遲加載 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

推薦答案

我最近一直在研究這個(gè)問(wèn)題,并查看了網(wǎng)絡(luò)上的大量示例代碼.沒有一個(gè)真正做到延遲加載(盡管聲稱)或具有比我愿意忍受的更復(fù)雜(不必要的花里胡哨).PhotoPicker 的 Apple WWDC 視頻似乎顯示延遲加載,但似乎更側(cè)重于預(yù)切片圖像的平鋪,因此沒有太大幫助.

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,因?yàn)樗鼈兒苄〔⑶也徽加媚敲炊鄡?nèi)存,然后加載 fullScreenImage scrollViewDidEndDecelerating 按需表示并重新加載 aspectThumbnail 以使 imageView 離開屏幕.效果非常流暢和視覺上,圖像有點(diǎn)粗糙,然后很快(通過(guò)背景加載)被更高分辨率的圖像替換.

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.

它可能會(huì)使用更多的細(xì)化 - 可能會(huì)為 currentPage +1 加載全分辨率圖像 &-1.我還沒有解決這個(gè)問(wèn)題.此外,我不完全確定我對(duì)塊的使用是否處于最佳狀態(tài)——但沒有出錯(cuò).

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 調(diào)用它并在我的根 viewController 上的 prepareForSegue: 方法中設(shè)置 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];
}

這篇關(guān)于uiscrollview 懶加載的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設(shè)置滾動(dòng)視圖內(nèi)容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自動(dòng)滾動(dòng))
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 模擬器上運(yùn)行時(shí),使用 iOS 6.0 SDK 并為 iOS 5 Target 構(gòu)建會(huì)導(dǎo)致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創(chuàng)建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
主站蜘蛛池模板: 欧美男人亚洲天堂 | 国产精品毛片在线 | 国产高清一区二区三区 | 欧美在线 | 欧美日韩大陆 | 美日韩精品| 亚洲国产精品视频一区 | 在线成人免费视频 | 在线观看亚 | 日韩精品久久久 | 天天综合久久 | 精品日韩一区二区 | 狠狠躁躁夜夜躁波多野结依 | 超碰av在线 | 国产成人精品久久二区二区91 | 精品一区二区三区电影 | 激情网站| www狠狠爱com | a级在线| jdav视频在线观看免费 | 亚洲区一区二 | 久久日本 | 午夜激情国产 | 亚洲一区在线日韩在线深爱 | www.久久精品 | 色av一区二区 | 国产精品久久久久久亚洲调教 | 亚洲毛片网站 | 亚洲精品一二三区 | 国产区高清 | 99热播精品 | 一区二区三区四区不卡 | 欧美极品少妇xxxxⅹ免费视频 | 久久久久久九九九九九九 | 成人激情视频在线观看 | 91亚洲国产亚洲国产 | 国产99久久精品一区二区永久免费 | 国产成人99久久亚洲综合精品 | 久久久精品视频免费看 | 中文字幕1区 | 一区二区福利视频 |