Displaying interlaced (progressive) Image in UIImageView(在 UIImageView 中显示隔行(逐行)图像)
问题描述
我正在尝试使用部分数据在下载时显示 JPEG 图像,类似于许多网络浏览器或 facebook 应用程序.
有一个低质量版本的图像(只是数据的一部分),然后以全质量显示完整的图像.
这在 VIDEO HERE
我关注了这个 SO 问题:
如何在 UIImageView 下载时显示渐进式 JPEG?
但我得到的只是一个图像视图,它随着数据的进入而被渲染,首先没有低质量版本,没有真正的渐进式下载和渲染.
谁能分享一个代码片段或指出我在哪里可以找到有关如何在 iOS 应用中实现此功能的更多信息?
例如尝试显示 JPEG 信息的链接,它将图像识别为渐进式
http://www.webpagetest.org/jpeginfo/jpeginfo.php?url=http://cetus.sakura.ne.jp/softlab/software/spibench/pic_22p.jpgp>
我使用了正确的代码序列
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data{///追加数据[_dataTemp appendData:data];///获取下载的总字节数const NSUInteger totalSize = [_dataTemp 长度];///更新数据源,我们必须传递所有数据,而不仅仅是新字节CGImageSourceUpdateData(_imageSource, (CFDataRef)_dataTemp, (totalSize == _expectedSize) ? true : false);///我们知道图像的预期大小if (_fullHeight > 0 && _fullWidth > 0){[_imageView setImage:[UIImage imageWithCGImage:image]];CGImageRelease(图像);}}
但代码只在加载完成时显示图像,其他图像在下载时会显示,但只是从上到下,没有低质量版本,然后像浏览器一样逐步添加细节.
这里的演示项目
这是我有一段时间感兴趣的话题:似乎没有办法使用 Apple 的 API 来做你想做的事,但如果你可以在这方面投入时间,你也许可以让它发挥作用.
首先,您需要一个 JPEG 解码库:libjpeg 或 libjpeg-turbo.然后,您需要将它集成到您可以与 Objective-C 一起使用的东西中.有一个使用这个库的开源项目,PhotoScrollerNetwork,它利用 turbo 库即时解码非常大的 jpeg"" 下载时可以平移和缩放(PhotoScroller 是一个 Apple 项目,可以进行平移和缩放,但它需要预先平铺的图像).
虽然上述项目并不完全符合您的要求,但您应该能够解除大部分 libjpeg-turbo 接口来解码渐进式图像并在收到低质量图像时返回它们.看起来您的图像非常大,否则几乎不需要渐进式图像,因此您可能会发现上述使用项目的平移/缩放功能.
PhotoScrollerNetwork 的一些用户要求支持渐进式图像,但在网络上似乎很少使用它们.
第二个想法:如果您将使用您的网站来出售渐进式图像(我假设这是因为通常可以找到的图像很少),您可以采取完全不同的策略.
在这种情况下,您将构建一个您自己设计的二进制文件——其中包含 4 个图像.前四个字节将提供其后数据的长度(每个后续图像将使用相同的 4 字节前缀).然后,在 iOS 端,随着下载的开始,一旦你获得了第一张图片的完整字节,你可以使用这些来构建一个小的低分辨率 UIImage,并在接收下一张图片时显示它.当下一个完全到达时,您将使用较新的高分辨率图像更新低分辨率图像.有可能您可以使用 zip 容器并进行动态解压缩 - 不是 100% 确定的.在任何情况下,以上都是您问题的标准解决方案,并且可以为 libjpeg 提供几乎相同的性能,而且工作量要少得多.
I am trying to display a JPEG image as it downloads, using part of the data, similiar to many web browsers do, or the facebook app.
there is a low-quality version of the image(just part of the data) and then display the full image in full quality.
this is best shown in the VIDEO HERE
I followed this SO question:
How do I display a progressive JPEG in an UIImageView while it is being downloaded?
but all I got was a imageview that is being rendered as data keeps comes in, no low-quality version first, no true progressive download and render.
can anyone share a code snippet or point me to where I can find more info as to how this can be implemented in an iOS app ?
tried this link for example which shows JPEG info, it identifies the image as progressive
http://www.webpagetest.org/jpeginfo/jpeginfo.php?url=http://cetus.sakura.ne.jp/softlab/software/spibench/pic_22p.jpg
and I used the correct code sequence
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
/// Append the data
[_dataTemp appendData:data];
/// Get the total bytes downloaded
const NSUInteger totalSize = [_dataTemp length];
/// Update the data source, we must pass ALL the data, not just the new bytes
CGImageSourceUpdateData(_imageSource, (CFDataRef)_dataTemp, (totalSize == _expectedSize) ? true : false);
/// We know the expected size of the image
if (_fullHeight > 0 && _fullWidth > 0)
{
[_imageView setImage:[UIImage imageWithCGImage:image]];
CGImageRelease(image);
}
}
but the code only shows the image when it is finished loading, with other images, it will show it as it downloading, but only top to bottom, no low quality version and then progressively add detail as browsers do.
DEMO PROJECT HERE
This is a topic I've had some interest in for a while: there appears to be no way to do what you want using Apple's APIs, but if you can invest time in this you can probably make it work.
First, you are going to need a JPEG decoding library: libjpeg or libjpeg-turbo. You will then need to integrate it into something you can use with Objective-C. There is an open source project that uses this library, PhotoScrollerNetwork, that uses leverages the turbo library to decode very large jpegs "on the fly" as they download, so they can be panned and zoomed (PhotoScroller is an Apple project that does the panning and zooming, but it requires pre-tiled images).
While the above project is not exactly what you want, you should be able to lift much of the libjpeg-turbo interface to decode progressive images and return the low quality images as they are received. It would appear that your images are quite large, otherwise there would be little need for progressive images, so you may find the panning/zooming capability of the above project of use as well.
Some users of PhotoScrollerNetwork have requested support for progressive images, but it seems there is very little general use of them on the web.
EDIT: A second idea: if it's your site that you would use to vend progressive images (and I assume this since there are so few to be found normally), you could take a completely different tact.
In this case, you would construct a binary file of your own design - one that had say 4 images inside it. The first four bytes would provide the length of the data following it (and each subsequent image would use the same 4-byte prefix). Then, on the iOS side, as the download starts, once you got the full bytes of the first image, you could use those to build a small low res UIImage, and show it while the next image was being received. When the next one fully arrives, you would update the low res image with the newer higher res image. Its possible you could use a zip container and do on the fly decompression - not 100% sure. In any case, the above is a standard solution to your problem, and would provide near-identical performance to libjpeg, with much much less work.
这篇关于在 UIImageView 中显示隔行(逐行)图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 UIImageView 中显示隔行(逐行)图像
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01