Loading images in background to optimize the loading in ios

后端 未结 9 1264
星月不相逢
星月不相逢 2021-02-02 03:02

I am trying to optimize the load in my application, in fact, I have a lot of images that loaded in my application, and I spend a lot of time waiting for a view controller to ope

9条回答
  •  名媛妹妹
    2021-02-02 03:31

    Use IDAsyncImageView.h

     //////////////////////////////////////
        IDAsyncImageView.h
     /////////////////////////////////////
    
            @interface IDAsyncImageView : NSObject
    
            @property (nonatomic, strong) NSCache *cache;    
    
            + (instancetype)instance;
    
            - (void)loadImageView:(UIImageView*)imageView withURLString:(NSString *)urlString;
    
            @end
    
        //////////////////////////////////////
        IDAsyncImageView.m
        /////////////////////////////////////
    
            #import "IDAsyncImageView.h"
    
    
    
                @implementation IDAsyncImageView
    
                    + (instancetype)instance
                    {
                        static IDAsyncImageView *_instance = nil;
                        static dispatch_once_t onceToken;
                        dispatch_once(&onceToken, ^{
                            _instance = [[self alloc] init];
                    });
                    return _instance;
                }
    
            - (instancetype)init
            {
                self = [super init];
                if (self) {
                    self.cache =  [[NSCache alloc] init];
                }
    
                return self;
            }
    
            - (void)loadImageView:(UIImageView*)imageView withURLString:(NSString *)urlString
            {
                UIActivityIndicatorView* activityView;
                activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
                activityView.hidesWhenStopped = YES;
                activityView.center = CGPointMake(imageView.bounds.size.width / 2.0f, imageView.bounds.size.height / 2.0f);
                activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
                [imageView addSubview:activityView];
                [activityView startAnimating];
    
                UIImage* imageLoad = [self.cache objectForKey:urlString];
                if (nil != imageLoad) {
                    imageView.image = imageLoad;
                    [activityView removeFromSuperview];
                }
                else {
    
                // Block variable to be assigned in block.
                __block NSData *imageData;
    
                // Dispatch a background thread for download
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                    imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
                    UIImage* imageLoad = [[UIImage alloc] initWithData:imageData];
    
                    // Update UI on main thread
                    dispatch_async(dispatch_get_main_queue(), ^(void) {
                        [self.cache setObject:imageLoad forKey:urlString];
                        imageView.image = imageLoad;
                        [activityView removeFromSuperview];
                    });
                });
                }
            }
    
        //////////////////////////////////////
        ViewController.m
        //////////////////////////////////////
    
            - (void)viewDidLoad {
            [super viewDidLoad];
            [[IDAsyncImageView instance] loadImageView:myImageView withURLString:aUrl];
            }
    

提交回复
热议问题