私は初心者で、これが私の最初の投稿です。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を使用しない場合、「reloadData」の方法は?
- 編集 -
- (void)viewDidLoad
{
[super viewDidLoad];
self.someArray = [[NSMutableArray alloc]init];
[self sampleCode];
NSLog([NSString stringWithFormat:@"Array - %@",self.someArray]);
// its still empty
}
そんな使い方できる?それはまだ空です
ありがとう