2

私は初心者で、これが私の最初の投稿です。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
}

そんな使い方できる?それはまだ空です

ありがとう

4

4 に答える 4

1

理由はありarrayませんallocated

メソッドでこれらの行を使用viewDidLoad:して割り当て、クラスのどこでも使用できるようにする

self.someArray = [[NSMutableArray alloc]init];

編集:あなたReloadingのにdata依存しますrequirement。たとえば、あなたが持っているとしましょうUIImageView *imgView

imgView.image = [UIImage imageNamed:@"someImage"];

successfully取得後は次のようupadatedになります。

imgView.image = someImageDownloaded using AFImageRequestOperation
于 2013-05-01T11:48:42.393 に答える
0

まず、あなたの self.someArray は初期化されていますか?

UITableView がない場合reloadDataは、成功ブロックでデータをロードするメソッドを呼び出し、失敗ブロックでエラーを処理するメソッド (たとえば、ユーザーにメッセージを表示したり、特定のエラーに応じてデータ処理を変更したりする) を呼び出すことができます。

一般に、成功ブロックが呼び出された場合、それは操作が成功したことを意味し、あなたの場合は画像の要求が成功したことを意味しますが、それは常に画像が受信されたことを意味するとは限りません。サーバーが成功を返す可能性がありますこの場合、画像はありませんが、成功ブロックが呼び出されます。

于 2013-05-01T11:45:47.597 に答える
0

テーブル ビューを使用しない場合は、URL からデータを取得した後、独自のメソッドを作成してデータを設定する必要があります。

于 2013-05-01T13:06:22.520 に答える
0

あなたはうまくやっていますが、プリンスがあなたの配列を割り当てるのを忘れたと言っていると思いますself.someArray

self.someArray = [[NSMutableArray alloc]init];//in ViewDidLoadMethod


NSURL *theUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://images.mathrubhumi.com/english_images/2012/Dec/06/03082_185786.jpg"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:theUrl];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil
                                      success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                                      UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
                                      imgV.image = image;
                                      [self.view addSubview:imgV];

                                      [self.someArray addObject:image];
                                      [self.someArray addObject:@"xx"];
                                      NSLog(@"arr--%@",self.someArray);
                                      imageView.image = image;// here imageView is globalVariable

                                      }
                                     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                     }];
[operation start];

これはself.someArray値を与えており、imageViewはその画像を表示しています。

于 2013-05-01T12:40:53.913 に答える