简体   繁体   English

网址显示一半图片

[英]Half Image display from url

在此处输入图片说明

Hi, 你好

i Asynchronous load image from url, problem is that image just load half i am not getting why this happening ,after some search on net i get two three reason for that, 我从网址异步加载图片,问题是图片仅加载一半,我不明白为什么会发生这种情况,在网上进行一些搜索后,我得到了两个三个原因,

1 it may happen because of sever side problem. 1它可能由于严重的侧面问题而发生。

2 it may happen because of image formate (but its happening for both the formate(png & jpg) so i thing that is not issue). 2它可能是由于图像甲酸盐而发生的(但是对于甲酸盐(png和jpg)都发生了,所以我认为这不是问题)。

3 it may happen because of too large size of image (size of images between 200kb to 700kb) 3可能由于图像太大(200kb至700kb之间的图像大小)而发生

its also happening in browser but sometimes. 它也在浏览器中发生,但有时会发生。

if you have any solution or advise regarding this issue than plz replay. 如果您有任何解决方案或建议,而不是plz重播。

Use this code.. It may be useful for you 使用此代码。可能对您有用

 NSString *htmlString = [NSString stringWithFormat:
                  @"<html>"
                  "<head>"
                  "<script type=\"text/javascript\" >"
                  "function display(img){"
                  "var imgOrigH = document.getElementById('image').offsetHeight;"
                  "var imgOrigW = document.getElementById('image').offsetWidth;"
                  "var bodyH = window.innerHeight;"
                  "var bodyW = window.innerWidth;"
                  "if((imgOrigW/imgOrigH) > (bodyW/bodyH))"
                  "{"
                  "document.getElementById('image').style.width = bodyW + 'px';"
                  "document.getElementById('image').style.top = (bodyH - document.getElementById('image').offsetHeight)/2  + 'px';"
                  "}"
                  "else"
                  "{"
                  "document.getElementById('image').style.height = bodyH + 'px';"
                  "document.getElementById('image').style.marginLeft = (bodyW - document.getElementById('image').offsetWidth)/2  + 'px';"
                  "}"
                  "}"
                  "</script>"
                  "</head>"
                  "<body style=\"margin:0;width:100%;height:100%;\" >"
                  "<img id=\"image\" src=\"%@\" onload=\"display()\" style=\"position:relative\" />"
                  "</body>"
                  "</html>",pass your url string here];

I was also facing these types of problem, it is basically the error of network when the asyncronous thread not able to download the image completely. 我也面临这些类型的问题,当异步线程不能完全下载图像时,基本上是网络错误。 The solution is to check whether downloaded image is valid or not. 解决方法是检查下载的图像是否有效。 if not valid then download again. 如果无效,请重新下载。 Below is the method i used to check whether image is valid or not: 下面是我用来检查图像是否有效的方法:

Note: this check is only for PNG images, To check JPEG just replace first and last two bytes in "if" condition. 注意:此检查仅适用于PNG图片,要检查JPEG,只需在“ if”条件下替换前两个字节即可。

- (BOOL)isImageValid:(NSData *)data
{
    BOOL val = YES;

    if ([data length] < 4) 
        val = NO;

    const unsigned char * bytes = (const unsigned char *)[data bytes];

    if (bytes[0] != 0x89 || bytes[1] != 0x50) 
        val = NO;
    if (bytes[[data length] - 2] != 0x60 || 
        bytes[[data length] - 1] != 0x82) 
        val = NO;

    return val; //return YES if valid 
}

Don't forget to accept the answer. 不要忘记接受答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM