I'm trying to load an image that will be displayed at the background of my app by using AFNetworking. The problem comes when the viewDidLoad calls to load the image, the AFNetworking is not done loading the data, so it doesn't display.

Here my Code.

AFNetworking:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    // 3
    NSDictionary *user = (NSDictionary *)responseObject;

    NSString *backurlJSON=[user valueForKeyPath:@"back_url"][0];
    NSLog(@"Background from Start: %@",backurlJSON);

    if(![backurlJSON isEqualToString:@""]){

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.mydomain.com/images/%@", backurlJSON]];
         NSData *data = [[NSData alloc]initWithContentsOfURL:url ];
         imgBack = [[UIImage alloc]initWithData:data ];

        backgroundView = [[UIImageView alloc] initWithImage: imgBack];

So in the viewDidLoad I have the subview UIImageView loading an image, in case there is no image to load, that I will like to overwrite with the one coming from the AFNetworking.

ViewDidLoad:

background = [UIImage imageNamed: @"2.png"];
backgroundView = [[UIImageView alloc] initWithImage: background];
backgroundView.frame = CGRectMake(-10, -10, 340, 588);
backgroundView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:backgroundView];

Any idea how to do this?

有帮助吗?

解决方案

With your code, you are using AFNetworking to download a JSON file with the 'back_url', then you are downloading the image in the main thread, instead of this code:

if(![backurlJSON isEqualToString:@""]){

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.mydomain.com/images/%@", backurlJSON]];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url ];
    imgBack = [[UIImage alloc]initWithData:data ];

    backgroundView = [[UIImageView alloc] initWithImage: imgBack];
}

You can use something like:

    NSString *backurlJSON=[user valueForKeyPath:@"back_url"][0];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.mydomain.com/images/%@", backurlJSON]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];


    AFHTTPRequestOperation *postOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    postOperation.responseSerializer = [AFImageResponseSerializer serializer];
    [postOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        backgroundView.image = responseObject;

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Image error: %@", error);
    }];

其他提示

It looks like you're overwriting your UIImageView and never adding it back to the view hierarchy. Try changing the image property of your image view instead of creating a new one:

[backgroundView performSelectorOnMainThread:@selector(setImage:) withObject:imgBack waitUntilDone:NO];

instead of

backgroundView = [[UIImageView alloc] initWithImage: imgBack];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top