Loading images in background to optimize the loading in ios

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

    Yes, you can add placeholder image to it.

    It will not slow down the scrolling and will load image accordingly with time.

    Also import this file UIImageView+WebCache.m and MapKit Framework

    Here is the link to download the files.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         static NSString *CellIdentifier = @"Cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            }
        UIImageview*  iV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
       [iV setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"image_placeholder.gif"]];
       [cell.contentView addSubview:iV];
       [iV release];
    
    }
    

    Just clean, build and run.

    0 讨论(0)
  • 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;
        });
    });
    }
    
    0 讨论(0)
  • 2021-02-02 03:48

    You can use async imageview.

    - (void) loadImageFromURL:(NSURL*)url placeholderImage:(UIImage*)placeholder cachingKey:(NSString*)key {
        self.imageURL = url;
        self.image = placeholder;
    
        NSData *cachedData = [FTWCache objectForKey:key];
        if (cachedData) {   
           self.imageURL   = nil;
           self.image      = [UIImage imageWithData:cachedData];
           return;
        }
    
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
            NSData *data = [NSData dataWithContentsOfURL:url];
    
            UIImage *imageFromData = [UIImage imageWithData:data];
    
            [FTWCache setObject:data forKey:key];
    
            if (imageFromData) {
                if ([self.imageURL.absoluteString isEqualToString:url.absoluteString]) {
                    dispatch_sync(dispatch_get_main_queue(), ^{
                        self.image = imageFromData;
                    });
                } else {
    //              NSLog(@"urls are not the same, bailing out!");
                }
            }
            self.imageURL = nil;
        });
    }
    

    Take a look at this link.You will have an idea on using async imageview.

    0 讨论(0)
提交回复
热议问题