iosobjective-curlparse-platformpffile

get image faster than file URL


I'm creating a stack of images in my viewdidload method. The images are from PFFile's from parse so they contain the fileURL of my image data.

My problem is that these two lines of code are dramatically slowing down my app and killing my user experience:

    //get scene object
    PFObject *sceneObject = self.scenes[i];


    //get the PFFile and filetype
    PFFile *file = [sceneObject objectForKey:@"file"];
    NSString *fileType = [sceneObject objectForKey:@"fileType"];

    //check the filetype
    if ([fileType  isEqual: @"image"])
    {
        //get image
        NSURL *imageFileUrl = [[NSURL alloc] initWithString:file.url];  
        NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl]; ********** these
        imageView.image = [UIImage imageWithData:imageData];  ********************* lines

    }

How do I get this image/these images (this is nested in a for loop) more quickly? I've already downloaded my PFObjects that contain the PFFiles and stored them locally.

I guess I really don't understand how file URL's operate.

Thank you.


Solution

  •     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{             
            NSURL *imageFileUrl = [[NSURL alloc] initWithString:file.url];  
            NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl]; 
            dispatch_get_main_queue(), ^{
               imageView.image = [UIImage imageWithData:imageData];
            });
        });
    

    Have not tested but this is the gist. Get the file loading off the main queue and make it asynchronous. The UI will not be bogged down because as soon as this queue is dispatched it's going to return and keep evaluating the rest of your application.