How to retrieve images from server asynchronously

后端 未结 5 2027
难免孤独
难免孤独 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:27

    You could download image asynchronously using GCD. Use the following code,

    __block NSData *imageData;
    
    dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, NULL);
    dispatch_async(myQueue, ^{
      //load url image into NSData
      imageData = [NSData dataWithContentsOfURL: your_image_URL];
      if(imageData) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            //convert data into image after completion
            UIImage *img = [UIImage imageWithData:imageData];
            //do what you want to do with your image
        });
     } else {
          NSLog(@"image not found at %@", your_image_URL);
     }
    });
    dispatch_release(myQueue);
    

    For further info, see dispatch_queue_t

提交回复
热议问题