简体   繁体   中英

Why do I get this error when trying to return a UIImage fetched from Parse.com?

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?

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
        });
    }
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM