您应该查看Cocoa Helpers - 它具有用于异步加载的 SimpleHTTPLoader 和完全满足您需要的 ImageViewCached。您创建的不是 imageView,而是 ImageViewCached,只需为其设置 URL。它会为您完成其余的工作。
但是,如果您想按照自己的方式进行操作:
我不保证下面的代码是正确的,但整个想法是(我从工作代码中获取并重写它以对应您的任务。我使用可可助手进行 base64 解码。您可以使用自己的方法。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *query = [NSString stringWithFormat:@"site.example/image.jpg"];
NSString *data = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] autorelease];
theImage = [UIImage imageWithData:(rfc::from_base64(data))]
dispatch_sync(dispatch_get_main_queue(), ^{
self.imageview.image = theImage;}
});
});
PS 对于 base64 编码的图像,您必须修改 ImageViewCached 才能加载和解码图像。