0
votes

I am getting an error when trying to return a UIImage fetched from Parse.com for this code below:

-(UIImage *)getUserImageForUser:(PFUser *)user {


    PFFile *userImageFile = user[@"profilePic"];
    [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (!error) {
            UIImage *image = [UIImage imageWithData:imageData];
            [self.images setObject:image forKey:user.username];
            return image;

        }
    }];
    return nil;
}

The error is: Incompatible block pointer types sending 'UIImage *(^)(NSData *__strong, NSError *__strong)' to parameter of type 'PFDataResultBlock' (aka 'void (^)(NSData *__strong, NSError *__strong)')

If I remove the UIImage return in the block there is no error. Not sure why this is happening. I am covering all bases by returning nil at the end. Can anyone give me a pointer as to why this is happening please?

1

1 Answers

2
votes

Because you don't return anything from within the code block. That code block runs, and thats it, you don't need to return anything there.

If you want to do something with that image, do it inside the code block.

For example:

[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
    if (!error) {
        UIImage *image = [UIImage imageWithData:imageData];
        dispatch_async(dispatch_get_main_queue(), ^{
            // do something with image on the main queue, like: 
            self.imageView.image = image
        });
    }
}];