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 :)

有帮助吗?

解决方案

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 :)

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top