How to retrieve images from server asynchronously

后端 未结 5 2028
难免孤独
难免孤独 2021-01-14 06:03

i have one NSMutableArray with some image url\'s. The images have sizes between 12KB to 6MB. I use AsycImageView class and implement but when large

5条回答
  •  余生分开走
    2021-01-14 06:22

    Use GCD for lazy loading.

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
     dispatch_async(queue, ^{
           NSString *strURL = url here;
           NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
           UIImage *image = nil;
           if(data)
            image = [UIImage imageWithData:data];
          dispatch_sync(dispatch_get_main_queue(), ^{
               //now use image in image View  or anywhere according to your requirement.
               if(image)
                 yourImgView = image
          });
     });
    

提交回复
热议问题