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

后端 未结 3 1926

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:41

    You cannot return an image from inside a block. This is an asynchronous API and cannot be used in the manner you are attempting to. Either use a blocking API, where the method is blocking until the image is downloaded (a bad solution), or implement support for the asynchronous API. For instance, pass a completion block to your getImage method and call it with in the completion block of the download operation. In this block, do what you need with the image.

    0 讨论(0)
  • 2020-12-12 08:41

    You are calling an asynchronous method with two blocks for handling success and failure. By the time one of these handlers is called, your calling method is long gone. It doesn't make any sense to think you could return data to it, because it is gone.

    In the success block and failure block, you give instructions what to do when the operation has succeeded or failed. There is nobody there to return anything to. What you do is add code in the block to process and store the result of success in the right place, or to handle errors in the correct way.

    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题