Dispatch_async to get multiple images from url

I want to get many images from different url and set it as image buttons. I tried to do it as shown above but nothing happens. When I access this view controller it has no image for the buttons and no banner view is displayed ....

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

            NSURL *url = [NSURL URLWithString: [pictureUrl objectAtIndex:i]];
            NSData *data = [[NSData alloc] initWithContentsOfURL:url];

            dispatch_async(dispatch_get_main_queue(), ^(void){

                UIImage *img1 = [[UIImage alloc]initWithData:data];
                img2.image = img1;
                [bt setBackgroundImage:img2.image forState:UIControlStateNormal];
  });
});

      

+3


source to share


5 answers


I recommend that you use a library that maintains a cache for images. For example, I used AFNetworking for one of my projects and it's awesome. And it is automatically processed in the background for you. In my case, I need a library that automatically cancels the request when I start a new one, and that worked for me. You can see the code here . And you can see a similar solution from another thread like this:

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Response: %@", responseObject);
    _imageView.image = responseObject;

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Image error: %@", error);
}];
[requestOperation start];

      



Hope this helps you.

0


source


IMHO you must use both pairs , the indoor unit must be in sync with the first one so that it gets an image when the data has been loaded and available. Also, don't forget to handle errors: async

sync

Try it like this:



// Show HUD here
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{

      NSError* error;
      NSURL *url = [NSURL URLWithString: [pictureUrl objectAtIndex:i]];
      NSData *data = [[NSData alloc] initWithContentsOfURL:url options:0 error:&error];

      dispatch_sync(dispatch_get_main_queue(), ^(void){

        if(!error && data)
        {
            UIImage *img1 = [[UIImage alloc] initWithData:data];
            img2.image = img1;
            [bt setBackgroundImage:img2.image forState:UIControlStateNormal]; 
             /*
                //----OR----
               [bt setBackgroundImage:img1 forState:UIControlStateNormal]; 
              */           
        }
        else
        {
            // Do error handling here
        }
        // Hide HUD here
  });
});

      

Hope it helps!

0


source


 imageView.image = [UIImage imageWithData:yourDefaultImgUrl];

   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *imageData = [NSData dataWithContentsOfURL:yourImageUrl];      
        if (imageData){
            dispatch_async(dispatch_get_main_queue(), ^{               
                 imageView.image = [UIImage imageWithData:imageData];
            });
        }
    });

      

Hope it helps.

0


source


Try this modification,

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

       NSURL *url = [NSURL URLWithString: [pictureUrl objectAtIndex:i]];
       NSError *error;
       NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];   
       if(error)
        {
           NSLog(@"Error: %@", [error localizedDescription]);
        }
        else
        {
          UIImage *img1 = [[UIImage alloc]initWithData:data];
          [bt setBackgroundImage:img1 forState:UIControlStateNormal];
        }
       // dispatch_async(dispatch_get_main_queue(), ^(void){
            //img2.image = img1;
       // });
});

      

Hope it helps.

-1


source


try it

dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
            dispatch_async(myQueue, ^{
                NSString *ImageURL = @"yourURL.jpg";
                NSData *imageData;
                if (ImageURL == nil || ImageURL == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",ImageURL] length] == 0 || [[[NSString stringWithFormat:@"%@",ImageURL] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
                {
                }

                else
                {
                    imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];

                }
                dispatch_async(dispatch_get_main_queue(), ^{

                    if (ImageURL == nil || ImageURL == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",ImageURL] length] == 0 || [[[NSString stringWithFormat:@"%@",ImageURL] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
                    {
                        imageView.image=[UIImage imageNamed:@"photo_frame_noimage.png"];
                    }
                    else if (imageData == nil || imageData == (id)[NSNull null] || [[NSString stringWithFormat:@"%@",imageData] length] == 0 || [[[NSString stringWithFormat:@"%@",imageData] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0)
                    {
                        imageView.image=[UIImage imageNamed:@"photo_frame_noimage.png"];
                    }
                    else
                    {
                        imageView.image=[UIImage imageWithData:imageData];
                    }
                });

            });

      

-1


source







All Articles