Uiscrollview lazy loading

前端 未结 4 1436
别跟我提以往
别跟我提以往 2021-01-06 20:59

I have got all images of iphone using AssetsLibrary.I am passing UIImage object in imageview and display images in scrollview. There are more than 200 images in iphone and i

4条回答
  •  耶瑟儿~
    2021-01-06 21:17

    You can use GCD block and grand central dispatch. in your .h file declare it

    dispatch_queue_t imageQueue_;
    

    Create an NSmutableDictionary

    @property (nonatomic, strong) NSMutableDictionary *thumbnailsCache;
    

    Use below Code to Show image.

    NSString *thumbnailCacheKey = [NSString stringWithFormat:@"cache%d",imageIndex];
    
        if (![[self.thumbnailsCache allKeys] containsObject:thumbnailCacheKey]) {
    
            // thumbnail for this row is not found in cache, so get it from remote website
            __block NSData *image = nil;
            dispatch_queue_t imageQueue = dispatch_queue_create("queueForCellImage", NULL);
            dispatch_async(imageQueue, ^{
                NSString *thumbnailURL = [NSString stringWithFormat:@"%@",deal_Class.deal_imageUrl];
                image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"Your URL"]];
                dispatch_async(dispatch_get_main_queue(), ^{
    
    
    
                  imageView.image = [UIImage imageWithData:image];
    
                    if (image) {
                        [self.thumbnailsCache setObject:image forKey:thumbnailCacheKey];
                    }
    
    
                });
            });
    
         //   dispatch_release(imageQueue);
    
    
        } else {
    
            // thumbnail is in cache
            NSData *image = [self.thumbnailsCache objectForKey:thumbnailCacheKey];
            dispatch_async(dispatch_get_main_queue(), ^{
                imageview.image = [UIImage imageWithData:image];
            });
        }
    

提交回复
热议问题