我是菜鸟,这是我在这里的第一篇文章。我试图从 URL 加载图像并将其分配给一些 NSMutableArray 或 UIImage 变量,但它失败了。我知道 AFNetworking 中的异步事情已经讨论了很多,但我仍然遗漏了一些东西。这是我的示例代码
-(void)sampleCode{
NSURL *theUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/images/054.jpg"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:theUrl];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
  imageProcessingBlock:nil
   success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
       //this code success
       NSLog([NSString stringWithFormat:@"--%@--", image]);
       UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
       imgV.image=image;
       [self.view addSubview:imgV];
       //End code success. Nslog and image appear
       //this code fail
       [self.someArray addObject:image];
       [self.someArray addObject:@"xx"];
       self.someImage = image;
       NSLog([NSString stringWithFormat:@"%@ --",self.someArray]);
       //end code fail. self.someArray and self.someImage null.
   }
   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
   }];
[operation start];
代码可以在成功块中使用图像。但我仍然需要处理图像,所以我需要将它分配给全局变量(如果是 NSMutableArray *someArray 或 UIImage *someImage)。
我的问题是:如果我可以在成功块中使用它,这意味着图像已到达,但为什么它不能分配给全局变量?
当我使用 UITableViewControler 时,我可以使用 self.tableView reloadData。当我不使用 tableView 时,如何“重新加载数据”?
- 编辑 -
- (void)viewDidLoad
{
[super viewDidLoad];
self.someArray = [[NSMutableArray alloc]init];
[self sampleCode];
NSLog([NSString stringWithFormat:@"Array - %@",self.someArray]);
// its still empty
}
我可以这样用吗?它仍然是空的
谢谢