How do I return a variable from a block inside a method?

后端 未结 3 1925

Say I have this method that given a URL returns a UIImage:

- (void)getUIImageFromURL:(NSURL *)URL {
    NSURLRequest *request = [NSURLRequest requestWithURL:         


        
3条回答
  •  有刺的猬
    2020-12-12 08:48

    The callback block in setCompletionBlockWithSuccess: is a void block, you can't change it's return type.

    In your case, you would probably set the image inside of your block, instead of returning an image.

    [imageOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.myImage.image = [UIImage imageWithData:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    

    However, if you are dealing with AFNetworking and images, there are category methods that should greatly simplify retrieval and cacheing.

    [self.myImage setImagewithURL:URL];
    

提交回复
热议问题