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

如果 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 回答
1

您必须在主线程上提交所有 UI 更新。例如,在回调块中设置图像:

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