Loading images in background to optimize the loading in ios

后端 未结 9 1261
星月不相逢
星月不相逢 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:25

    you can check this tutorial on NSOperationQueue and this on GCD doing exactly same. Also you can try using:

    // Block variable to be assigned in block.
    __block NSData *imageData;
    dispatch_queue_t backgroundQueue  = dispatch_queue_create("com.razeware.imagegrabber.bgqueue", NULL);
    
    // Dispatch a background thread for download
    dispatch_async(backgroundQueue, ^(void) {
        imageData = [NSData dataWithContentsOfURL:imageURL];
        UIImage *imageLoad;
        imageLoad = [[UIImage alloc] initWithData:imageData];
    
        // Update UI on main thread
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            imageView.image = imageLoad;
        });
    });
    

提交回复
热议问题