我试图加载将在我的应用程序的背景通过使用AFNetworking要显示的图像。问题是当该viewDidLoad
调用加载图像时,AFNetworking尚未完成加载数据,因此它不会显示。AFNetworking 2.0&背景图像
这里我的代码。
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];
所以在viewDidLoad
我有子视图UIImageView
加载图像,如果没有图像加载,我会喜欢从一来覆盖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];
任何想法如何做到这一点?
这项工作完美,谢谢! –