Reputation: 1658
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.
2 it may happen because of image formate (but its happening for both the formate(png & jpg) so i thing that is not issue).
3 it may happen because of too large size of image (size of images between 200kb to 700kb)
its also happening in browser but sometimes.
if you have any solution or advise regarding this issue than plz replay.
Upvotes: 0
Views: 408
Reputation: 1154
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.
- (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.
Upvotes: 0
Reputation: 2451
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];
Upvotes: 1