下载时如何在 UIImageView 中显示渐进式 JPEG?

发布于 2024-10-13 01:38:47 字数 165 浏览 8 评论 0原文

从网上下载图像并将其显示在 UIImageView 中相当容易。然而,这需要在向用户显示图像之前完全下载图像,从而完全击败渐进式 JPEG(和 PNG)图像。

如何在传输完成时渲染部分下载的图像?我想象 SDK 有一些回调函数可以更新图像,但我找不到这样的函数。目前的 iOS SDK 是否可以实现?

Downloading an image from the net and showing it in an UIImageView is fairly easy. However, this requires the image to be completely downloaded before it is shown to the user, completely defeating progressive JPEG (and PNG) images.

How can I render the partially downloaded images while the transfer is being done? I would imagine the SDK to have some callback function which would update the image, but I can't find such a function. Is it possible at all with the current iOS SDK?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅听莫相离 2024-10-20 01:38:47

我知道这篇文章大约有 1 年的时间,但为了以防万一有人在寻找它,有一个名为 NYXImagesKit< /a> 这就是你正在寻找的。

它有一个名为 NYXProgressiveImageView 的类,它是 UIImageView 的子类。

您所要做的就是:

NYXProgressiveImageView * imgv = [[NYXProgressiveImageView alloc] init];
imgv.frame = CGRectMake(0, 0, 320, 480);
[imgv loadImageAtURL:[NSURL URLWithString:@"http://yourimage"]];
[self.view addSubview:imgv];
[imgv release];

此外,一个不错的选择是将图像保存为隔行扫描图像,以便以低质量加载并随着下载而改进。如果图像不是隔行扫描,则会从上到下加载。

I know this post has about 1 year, but just in case anyone is looking for it, there is a project called NYXImagesKit that does what you are looking for.

It has a class named NYXProgressiveImageView that is a subclass of UIImageView.

All you have to do is:

NYXProgressiveImageView * imgv = [[NYXProgressiveImageView alloc] init];
imgv.frame = CGRectMake(0, 0, 320, 480);
[imgv loadImageAtURL:[NSURL URLWithString:@"http://yourimage"]];
[self.view addSubview:imgv];
[imgv release];

Also, a good option is to save your images as interlaced so that it loads with low quality and improve with the download. If the image is not interlaced it is loaded from top to bottom.

黯然#的苍凉 2024-10-20 01:38:47

现在在 libjpeg-turbo 之上有一个小型开源库,可以轻松解码和显示渐进式 JPEG:

let imageView = CCBufferedImageView(frame: ...)
if let url = NSURL(string: "http://example.com/yolo.jpg") {
    imageView.load(url)
}

请参阅 https://github.com/contentful-labs/Concorde

There is now a small open-source library on top of libjpeg-turbo which allows decoding and displaying progressive JPEGs easily:

let imageView = CCBufferedImageView(frame: ...)
if let url = NSURL(string: "http://example.com/yolo.jpg") {
    imageView.load(url)
}

see https://github.com/contentful-labs/Concorde

青衫负雪 2024-10-20 01:38:47

您是否尝试在 didReceiveData 被调用时部分渲染 UIImage ...?像……

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //TODO: add some error handling in case the image could not be created
    UIImage *img=[UIImage imageWithData:data];
    if (img) {
        self.imageView.image=img;
    }
}

Did you try to render the UIImage partially as didReceiveData gets called … ? Something like …

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //TODO: add some error handling in case the image could not be created
    UIImage *img=[UIImage imageWithData:data];
    if (img) {
        self.imageView.image=img;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文