在 中显示图像使用 nsdata 标记
这可能吗?我的应用程序将从服务器下载一个 zip 文件,并将所有图像保存在一个数组中,如下所示:
ZipReadStream *read = [unzipFile readCurrentFileInZip];
NSMutableData *data = [[NSMutableData alloc] initWithLength:info.length];
int bytesRead= [read readDataWithBuffer:data];
if(bytesRead > 0)
{
[imagesData addObject:data];
[imagesName addObject:info.name];
}
然后我过滤要在 uiview 内显示的 w/c 图像和要在 uiwebview 内显示的 w/c 图像。我在 uiview 中显示图像,如下所示:
UIImage *imageForThisQuestion = [[UIImage alloc]initWithData:[imagesData
objectAtIndex:indexOfThisImage]];
这在 uiview 中工作得很好。但如何显示 uiwebview 中的一些图像?我可以在这里使用标签吗?我的 uiwebview 也可能以这种格式出现:
"blah blah blah blah <img src...> blah blah blah <img src...>"
is this possible? my app will download a zip file from the server and I save all the images in an array like this:
ZipReadStream *read = [unzipFile readCurrentFileInZip];
NSMutableData *data = [[NSMutableData alloc] initWithLength:info.length];
int bytesRead= [read readDataWithBuffer:data];
if(bytesRead > 0)
{
[imagesData addObject:data];
[imagesName addObject:info.name];
}
then I filter w/c images to be displayed inside the uiview and w/c images to be displayed inside the uiwebview. I display the images inside the uiview like this:
UIImage *imageForThisQuestion = [[UIImage alloc]initWithData:[imagesData
objectAtIndex:indexOfThisImage]];
this works fine inside the uiview. but how do I display the some of the images inside the uiwebview? can I use the tag here? and also my uiwebview might appear in this format:
"blah blah blah blah <img src...> blah blah blah <img src...>"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不想将图像保存在文件中以在 html 中引用它,例如 << img src="myimage.png">,对吗?您可以使用数据URI方案将图像编码为URI,并使用img标签,例如
< img src="数据:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" >
阅读此处< /a> 如何对图像的 NSData 进行 Base64 编码。
You don't want to save an image in file to refer on it in html like < img src="myimage.png">, right? You can use data URI scheme to encode your image into URI, and use img tag like
< img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" >
Read here how to base64 encode NSData of your image.
对于任何想要快速清晰的答案并阅读上面提供的整篇文章的人来说,这就是您想要使用的代码:
并将该字符串注入到任何
中。
元素。请注意 base64EncodedStringWithOptions 的 0 值,这可以帮助您避免生成巨大 Base64 字符串的图像崩溃。您还可以将 png 更改为任何支持的图像格式For anyone wanting a quick and clear answer and slip reading the whole article supplied above, this is the code you want to use:
and you inject that string to any
<img src="{{imgSrcValue}}">
element. Notice the 0 value to the base64EncodedStringWithOptions which may help you avoid crashes with images which produce huge Base64 strings. Also you can change the png to any supported image format您必须使用正确的
baseURL
加载 HTML 代码:然后,在您的 HTML 代码中:
You have to load the HTML code with the correct
baseURL
:Then, in your HTML code: