1

Im trying to create a image gallery using UICollectionView. But the dispatch_queue does not seem to download image instead it skips the download and continues executing the next statement.

Below is my code:

    for(int i=0;i<[_urlArray count];i++){
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[_urlArray objectAtIndex:i]]];

    NSData *data = [NSData dataWithContentsOfURL:url];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    path = [path stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
    NSLog(@"img data %@",data);
    [data writeToFile:path atomically:YES];

     UIImage *theImage=[UIImage imageNamed:path];
    [_imgArray addObject:theImage];
    dispatch_async(dispatch_get_main_queue(), ^{

    });
});
}

I even tried AFNetworking and SDWebImage. But did not help me. Please help me with this. Thank you.

Uday
  • 1,619
  • 3
  • 23
  • 48
  • is it executing up to NSData *data? If it is executing up to that line then check whether data is stored in the document directory or not. – Gyanendra Jul 02 '13 at 10:50
  • Nope its not downloading at all. It skips through next statement. – Uday Jul 02 '13 at 10:59
  • It will be downloading generally dispatch_queue will execute after few second so put the breakpoint inside the dispatch_queue that is after NSURL *url definitely it will execute it should not skip. – Gyanendra Jul 02 '13 at 11:09
  • yeah i get a null when tried print data. And it says variable is not NSData after adding a break point. – Uday Jul 02 '13 at 11:15
  • k check whether correct url you are getting or not. [NSString stringWithFormat:@"%@",[_urlArray objectAtIndex:i] this string you copy and paste it in your browser and load the url in browser check whether image is there or not. – Gyanendra Jul 02 '13 at 11:17
  • Yes im pretty sure its the same url. And i have verified it the url. It works fine. When i add just one url then it works fine. I get an image displayed on my collectionView but when pass an array of url's it skips. I even tried adding the same code inside cellForItemAtIndex: method, it did not work either. – Uday Jul 02 '13 at 11:20
  • K do one thing put the for loop inside the dispatch_queue instead of putting the dispatch_queue inside the for loop. Your code should look like this.dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{ for(int i=0;i<[_urlArray count];i++){ } dispatch_async(dispatch_get_main_queue(), ^{ }); }); try this and check. – Gyanendra Jul 02 '13 at 11:25
  • yes i did. Still the same :( May be its looping through very fast. – Uday Jul 02 '13 at 11:37

1 Answers1

0

Have you tried to store all data and Images from URL to iOS Cache memory or any DB?

Vaibhav Limbani
  • 777
  • 1
  • 12
  • 29
  • Thank you for the reply. No i am not caching it. Its just im parsing a remote json file and storing the image url's to an array. You can see from my code that urlArray is used to pass the url as a string to NSURL. – Uday Jul 02 '13 at 12:57
  • Hmmmm.. I've gone through this kind of dispatch_queue problem so,I think you should add your main code... to dispatch_async(dispatch_get_main_queue(), ^{ UIImage *theImage=[UIImage imageNamed:path]; [_imgArray addObject:theImage]; }); because your code seems partial ..I'm not sure but you should try this ..Best luck – Vaibhav Limbani Jul 02 '13 at 14:50
  • What do you suggest? I even tried SDWebImage, but downloads from the first url inside the array. And it never tries to download from the other urls. – Uday Jul 03 '13 at 06:57
  • can you tell me which portion of code goes here dispatch_async(dispatch_get_main_queue() ?? – Vaibhav Limbani Jul 03 '13 at 16:24
  • My url's had few space, because of which it was skipping through the code.Its fixed now. Thank buddy :) – Uday Jul 05 '13 at 09:32