我試圖加載將在我的應用程序的背景通過使用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];
任何想法如何做到這一點?
這項工作完美,謝謝! –