1

I don't have any clue what so ever and hence I am giving it up to some one who can assist me a proper way.Actually I have retrieved the Facebook friends details using FQL query,in which the profile picture is one of the field,to our inconvenience,the picture is retrieved in the form of an url.Earlier stages,I tried to fetch friends details using graph api(object),where it hits the server as many times as number of friends.Since it takes long time for FB friends to get synced,I moved forward to FQL and this is also annoying me like anything.Even though I have all the friends pictures in a single query,I am unable to convert them to images and save to documents folder rapidly.

I am having a Facebook sync button,in that button action,I am saving/updating the friends details in to database and simultaneously saving the friends pictures to documents.This is again leading me to similar problem,i.e. taking more time to sync.Here is my implementation code for understanding:

-(void)saveUpdateFriendDetails
{
    for (int index = 0; index<[friendsDetailsArray count]; index++)  
    {
       //Save or update friends details to db

       //Fetch friends picture url,convert to image and save to documents folder
       NSString *friendPictureString = [[self.friendsDetailsArray valueForKey:kPicture]objectAtIndex:index];
       NSURL *friendPhotoUrl = [NSURL URLWithString:friendPictureString];
       NSData *data = [NSData dataWithContentsOfURL:friendPhotoUrl];
       UIImage *friendProfilePicture = [UIImage imageWithData:data];

       NSString *filename = [facebookID stringByAppendingString:kPNG];

       //  Get the path of the app documents directory
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];

       //  Append the filename and get the full image path
       NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:filename];

       //  Now convert the image to PNG and write it to the image path
        NSData *imageData = UIImagePNGRepresentation(friendProfilePicture);
        [imageData writeToFile:savedImagePath atomically:NO];
}

I tried to run the process in background using GCD as well as AsyncImageView,but even then it is taking the same time,as the process remains the same(eventually we are running in background which is the only difference).

So is there any way to quickly convert the friends picture urls to photos and save to documents folder such that it doesn't affect my Facebook sync process.

Note: Without saving the pictures to documents,it is taking around 20-30 seconds for me to sync 800 friends and it is taking 2-3 minutes when saving images also.

Can some one please guide me.

Thanks every one in advance :)

Eshwar Chaitanya
  • 944
  • 2
  • 13
  • 34

2 Answers2

1

Oopa,I have found a solution to my own problem

Simply ignore saving the images to documents folder,that will increase our sync speed.

Later while displaying the details with images,simply retrieve the friend image using graph api,i.e. with one single line:

NSString *profilePicURL = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture",friendID];

and we can do the fetching stuff in background using GCD model for asynchronous loading,which will not let the performance slow down.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
        dispatch_async(queue, ^{
NSString *profilePicURL = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture",friendID];
NSURL *profilePhotoURL = [NSURL URLWithString:profilePicURL];
NSData *photoData = [NSData dataWithContentsOfURL:profilePhotoURL];
dispatch_sync(dispatch_get_main_queue(), ^{
//assign the image to cell here
image = [UIImage imageWithData:photoData];
cell.imageView = image;
 });
});

Thanks,hope this helps some one :)

Eshwar Chaitanya
  • 944
  • 2
  • 13
  • 34
  • +1 for you. Thats a nice code for loading the images fast. But i think thr is a problem. Once the view is loaded, it fetches all the friend names, but not the profile pics. only when we scroll, the table gets refreshed and the images then start to load. Any fix for that? – The X-Coder Jun 26 '13 at 12:37
  • @Ajitthala We are following asynchronous loading,hence the images load for visible view and fetches the images for invisible cells when we scroll down,it is recommended that asynchronous loading of images is the best way to eradicate performance and memory leaks issues :) – Eshwar Chaitanya Jun 26 '13 at 12:55
  • For me, images are not loading for visible view as well. Only when i scroll down, the images for the visible view is shown. – The X-Coder Jun 26 '13 at 13:25
  • Can you please provide me with your implementation code so that I can look at your problem – Eshwar Chaitanya Jun 26 '13 at 13:32
  • Please paste your code in pastebin.com and post the link here,I will go through it,no need of mailing your project please :) – Eshwar Chaitanya Jun 26 '13 at 14:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32426/discussion-between-ajit-thala-and-eshwar-chaitanya) – The X-Coder Jun 26 '13 at 14:32
0

Have you considered Image Caching, you would only need to store the URL (or construct it by userID) and load it only when needed.

Once the image is loaded once, you won't need to load it again (since it would be stored on the phone).

There is a nice library SDWebImage that does this.

Mariam K.
  • 590
  • 4
  • 11
  • Sorry,I didn't get you,did you mean to say,saving only the friends picture url – Eshwar Chaitanya Apr 23 '13 at 14:00
  • The value in this variable NSString *friendPictureString. – Mariam K. Apr 23 '13 at 14:04
  • How do you use these saved images later? – Mariam K. Apr 23 '13 at 14:07
  • But how do I assign the url according to table view row values,I mean in cell for row at index path method,we will access documents folder,but how do I provide the corresponding picture,I mean correct url for respective friend?? – Eshwar Chaitanya Apr 23 '13 at 14:07
  • you can store them in a plist or an array that's later saved to file. this is a [link](http://stackoverflow.com/a/8786191/1262527) to how to write an array to file as string – Mariam K. Apr 23 '13 at 14:13
  • Looks like I am finding a solution of my own Mr.Mariam,If at all I fail to succeed,will try to get back to you,thanks for the concern :) – Eshwar Chaitanya Apr 23 '13 at 14:15