1

私のビューではロードされました:URLから画像を取得する関数(非同期)を呼び出していますが、画像が表示される場合と表示されない場合があります

viewDidLoad の私のコード

if(entry.length > 0)
{
    [self getPicture];
}
else
{
    ProfilePicure.image = [UIImage imageNamed:@"icon_man.png"];
}
 [topView addSubview:ProfilePicure];
[self.view setBackgroundColor:UIColorFromRGB(0xffffff)];
[self.view addSubview:topView];

フェッチ機能:

   - (void) getPicture
  {
  [WebImageOperations loadFromURL:entry andBlock:^(UIImage *imageData).
  {
     if (self.view.window)
     {
         UIImage *image1 = imageData;
         ProfilePicure.image = image1;
     }
  }];
   //Tried to add those to be able to see the picture, but sometimes i can't see 
 [self.view setNeedsDisplay];
 [topView addSubview:ProfilePicure];
 [topView setNeedsDisplay];
  }

編集:フェッチ機能を追加して、このサイトからコピーし、それを使用して画像をフェッチしました。

   + (void)loadFromURL:(NSString *)urlString andBlock:(void (^)(UIImage *image))processImage
 {
     NSMutableString *urlSTR =[[NSMutableString alloc]init];
    [urlSTR appendString:@"http://www.ifat.com/files/infor/Person/"];
    [urlSTR appendString:urlString];
    NSURL *urlGet = [NSURL URLWithString:urlSTR];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
    NSData * imageData = [NSData dataWithContentsOfURL:urlGet];
    dispatch_async(dispatch_get_main_queue(), ^{
       UIImage * image = [UIImage imageWithData:imageData];
        processImage(image);
     });
});
}
4

2 に答える 2

1

メインスレッドですべての UI 更新をコミットする必要があります。たとえば、これを使用してコールバック ブロックに画像を設定します。

[ProfilePicture performSelectorOnMainThread:@selector(setImage:) withObject:image1 waitUntilDone:NO];
于 2013-11-12T07:41:26.920 に答える
1

画像を取得できないURLにsapceが含まれている場合は、URLを確認する必要がある場合があります。もしそうなら、あなたは使用する必要があります

urlSTR = [urlSTR stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

この行を後に置きます

[urlSTR appendString:@"http://www.ifat.com/files/infor/Person/"];
[urlSTR appendString:urlString];

確認してください。これは役立つかもしれません。

あなたは単純なことを複雑にしました。URLから画像を簡単にダウンロードできます..

NSMutableString *urlSTR =[[NSMutableString alloc]init];
[urlSTR appendString:@"http://smartradio.ch/APPETLM/admin/media_files/event/thumb/aaa162635eda024ee7dd908554b1b6cb.jpg"];
 NSURL *url = [NSURL URLWithString:[urlSTR stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    if ( data == nil )
        ProfilePicure.image = [UIImage imageNamed:@"icon_man.png"];
    dispatch_async(dispatch_get_main_queue(), ^{
        // WARNING: is the cell still using the same data by this point??
        ProfilePicure.image=[UIImage imageWithData:data];


    });

});

これをあなたのビューに入れてください。最初に静的URL画像で確認してから、URLを実装します

于 2013-11-12T08:36:35.117 に答える