Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Can we get the image data from a url

Does anyone have an idea if we could retrieve an image from parse using the url ,because i try to use the regular way it's too slow

2 Answers

how big are the images you are saving to parse?

a profile picture around 1 to 4 MB

its going to take some time to get any sort of data from your backend.

what you should do is cache the profile images so that you only have to download them once, then after its downloaded anytime you need it you just grab it from the local cache which is practically instant.

Ive been using SAMCACHE and it works pretty well and is very easy to use

you are going to have to come up with a way to uniquely identify each profile image and use that as a key in the cache. One way would be to use the users userID as the key.

so in your cellForRow method you check to see if [whatever userid for this tableCell] is in the cache and if it is just load it from there.

if its not in the cache yet you download it from the back end, then save it to the cache.

do you want an example in swift or objective c

objective c please

you would use something like this:

//get the profile photo from the cache using the users object id
UIImage *profilePhoto = [[SAMCache sharedCache] imageForKey:user.objectId];

        //if the photo was in the cache
        if (profilePhoto) {

            imageView.image = profilePhoto;

        } else {

            //get the useres photo in the background and cache it
            PFFile *file = [user objectForKey:"profilePhoto"];
            [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {

                UIImage *photo = [UIImage imageWithData:data];
                 //cache it for next time around
                [[SAMCache sharedCache] setImage:photo forKey:user.objectId];
                dispatch_async(dispatch_get_main_queue(), ^{
                    //get the main thread again so we can update the image view
                    imageView.image = photo;
                });


            }];

        }

this code will be placed in the tableview , so i don't have to change the code to save the image to parse right

this is just for retrieving the photo, not saving it to parse

Basically i don't need no code for the saving , and also i use PFImage so i replace the UIImage by it Am I right . and also i try to use your code it says undeclared SAMCACHE any ideas

if you are using PFImageView it will take care of caching and everything automatically

you just set the placeHolder image, the file, and then call the load in background method. if you call the load in background method for the same imageView after its already downloaded the image it simply loads it from the on disk cache.

PFImageView *imageView = [[PFImageView alloc] init];
imageView.image = [UIImage imageNamed:@"..."]; // placeholder image
imageView.file = (PFFile *)someObject[@"picture"]; // remote image

[imageView loadInBackground];