Loading images in background to optimize the loading in ios

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

    Here is a slightly modified approach of Ramu Pasupoletis answer. I added the __block modifier to make the var img visible inside the block called on the main thread. Here is the complete method definition which I use in

    -(UITableViewCell*)cellforRowAtIndexPath:(UIIndexPath*)indexPath;
    

    for fetching the thumbnails lazily. I also added placeholders there for the cells UIImageViews.

    //lazy loading of thumbnails for images in cell via bg thread
    -(void)loadImageForCell:(CustomEditTableViewCell*)theCell withFilepath: (NSString*)filepath{
    
    dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(q, ^{
        UIImage (__block *img) = [UIImage imageWithContentsOfFile:filepath];
        UIImage *thumbnail = [[GlobalFunctions sharedGlobalFunctions] imageOfSize:CGSizeMake(40, 40) fromImage:img];
        dispatch_async(dispatch_get_main_queue(), ^{
            theCell.imageView.image = thumbnail;
        });
    });
    }
    

提交回复
热议问题