Search code examples
iphoneobjective-ciosxcodeuiwebview

display images in <img> tag using nsdata


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...>"

Solution

  • 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.