简体   繁体   English

为什么尝试返回从Parse.com提取的UIImage时出现此错误?

[英]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: 尝试返回从Parse.com获取的以下代码的UIImage时出现错误:

-(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)') 错误是: 不兼容的块指针类型将'UIImage *(^)(NSData * __ strong,NSError * __ strong)'发送到类型'PFDataResultBlock'的参数(又名'void(^)(NSData * __ strong,NSError * __ strong)')

If I remove the UIImage return in the block there is no error. 如果删除块中的UIImage返回,则没有错误。 Not sure why this is happening. 不知道为什么会这样。 I am covering all bases by returning nil at the end. 我通过在最后返回nil覆盖所有基础。 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
        });
    }
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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