Loading images in background to optimize the loading in ios

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

    Try this code:

    dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(q, ^{
        /* Fetch the image from the server... */
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            /* This is the main thread again, where we set the tableView's image to
             be what we just fetched. */
            cell.imgview.image = img;
        });
    });
    

提交回复
热议问题