在我看来确实加载了:我需要一个从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);
});
});
}
最佳答案
您可能需要检查url,如果其中包含无法获取图像的sapce。如果是这样,那么您需要使用
urlSTR = [urlSTR stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
在这行之后
[urlSTR appendString:@"http://www.ifat.com/files/infor/Person/"];
[urlSTR appendString:urlString];
并检查一下。这可能会有所帮助。
您使简单的事情变得复杂。您可以轻松地从网址下载图片。
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];
});
});
放在您的视图中确实加载了。首先使用一些静态网址图片进行检查,然后实施您的网址