- Home /
How do I prevent Texture2D.EncodeToPNG from creating artifacts on iOS?
When I run the code block below using a standalone build or from within the editor the resulting PNG images look perfect. When executed on an iPad or iPhone the resulting PNG has artifacts as if it was being compressed. I'm at a loss.
...
Texture2D image;
for(int i=0; i<Grid.UptPixelList.Count; i++){
image = new Texture2D(imageWidth, imageHeight, TextureFormat.ARGB32, false);
image.filterMode = FilterMode.Point;
pixelList = Grid.UptPixelList[i];
origin = Grid.OriginList[i];
foreach(UptPixel pixel in pixelList){
if(Mathf.FloorToInt(pixel.position.z - origin.z) < 0) continue; // Skip, we've gone underground.
image.SetPixel(Mathf.FloorToInt(pixel.position.x - origin.x + halfWidth), Mathf.FloorToInt(pixel.position.z - origin.z), pixel.color);
}
image.Apply();
File.WriteAllBytes(filename + Path.DirectorySeparatorChar + dirname + layerCounter + ".png", image.EncodeToPNG());
Destroy(image);
layerCounter++;
}
...
If you would display the image using GUI.DrawTexture etc, does it also have artefacts? Could you show us the images you generate?
I won't be able to do the GUI Texture test until $$anonymous$$onday but for now here is the standalone and ios export of a simple red square for comparison.
Answer by crappygraphix · Apr 08, 2013 at 05:50 AM
So the problem was a simple one to solve. Basically the foreach loop going through "pixelList" in the code shown in the question above only writes to a subset of the pixels in the texture. I needed to initialize all pixels of the texture to clear before writing the pixels in the list.
The new code looks like this:
...
Texture2D image = new Texture2D(imageWidth, imageHeight, TextureFormat.ARGB32, false);
image.filterMode = FilterMode.Point;
for(int i=0; i<Grid.UptPixelList.Count; i++){
ClearImage(image, imageWidth, imageHeight);
pixelList = Grid.UptPixelList[i];
origin = Grid.OriginList[i];
foreach(UptPixel pixel in pixelList){
if(Mathf.FloorToInt(pixel.position.z - origin.z) < 0) continue; // Skip, we've gone underground.
image.SetPixel(Mathf.FloorToInt(pixel.position.x - origin.x + halfWidth), Mathf.FloorToInt(pixel.position.z - origin.z), pixel.color);
}
image.Apply();
File.WriteAllBytes(filename + Path.DirectorySeparatorChar + dirname + layerCounter + ".png", image.EncodeToPNG());
layerCounter++;
}
Destroy(image);
...
Where ClearImage is:
void ClearImage(Texture2D image, int width, int height){
for(int x=0; x<width; x++){
for(int y=0; y<height; y++){
image.SetPixel(x,y,Color.clear);
}
}
image.Apply();
}
I had the same thing but personally I found I had to initialize each pixel as a new Color(1, 1, 0.005f)
because otherwise it would just overwrite the png with blank and do the same thing. It was doing this weird stretching thing where a png with a few non-transparent pixels would create this nightmarish texture
Your answer
![](https://koobas.hobune.stream/wayback/20220613095720im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Texture2D.EncodeToPNG not working on iOs on Unity 3.5.5f3 1 Answer
Bug - Texture "corrupted" after sending on server with MultipartformDataSection, only on iOS 1 Answer
EncodeToPng that works more smoothly on mobile 1 Answer
iOS Sprites in PVRTC? 1 Answer
Only RGBA32bit works when setting Texture2D pixels on iOS? 0 Answers