Hi all!
I’m having a problem and I hope someone can help me. I would be so grateful.
I have a desktop app where the user can upload a texture to a model. I’m saving this texture so the user can load it at a later time. My problem is when I try to publish to ios. I have change file to cameraroll, so browsing for the image works. The problem appears when I try to save and apply the texture, then the app freezes. Here is the code that works for the desktop (iv’e added some code to the final function so the texture that is uploaded doesn’t have to be aspect 1:1):
function browseTexturesClicked()
{
textureFile = new File();
textureFile.browseForOpen("Open", [new FileFilter("JPG", "*.jpg")]);
textureFile.addEventListener(Event.SELECT, onTextureSelected);
}
function onTextureSelected(evt:Event):void
{
loadPreset = false
textureFile.addEventListener(Event.COMPLETE, textureLoaded);
textureFile.load();
textureFile.removeEventListener(Event.SELECT, onTextureSelected);
}
function textureLoaded(evt:Event):void
{
removeEventListener(Event.ENTER_FRAME, render);
var ba:ByteArray = textureFile.data;
textureLoader = new Loader();
textureLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, textureMaterial);
textureLoader.loadBytes(ba);
textureFile.removeEventListener(Event.COMPLETE, textureLoaded);
}
function textureMaterial(evt:Event):void
{
var bitmapToSave:ByteArray = textureFile.data;
var bitmapFileName:String = "temptextures/" + "texture" + (currSelectedTexture+1).toString() + ".jpg"
var bitmapFile:File = File.applicationStorageDirectory.resolvePath(bitmapFileName);
var bitmapFileStream:FileStream = new FileStream();
bitmapFileStream.open(bitmapFile, FileMode.WRITE);
bitmapFileStream.writeBytes(bitmapToSave, 0, bitmapToSave.length);
bitmapFileStream.close()
currSelectedModelXml.children()[4].children()[currSelectedTexture].children()[1] = "texture" + (currSelectedTexture+1).toString() + ".jpg"
var shortSide:Number
var longSide:Number
var widthLongest:Boolean = true
longSide = evt.target.content.bitmapData.width
shortSide = evt.target.content.bitmapData.height
if(shortSide > longSide)
{
widthLongest = false
shortSide = evt.target.content.bitmapData.width
longSide = evt.target.content.bitmapData.height
}
var divideNumLarge:Number = 1024/longSide
var divideNumSmall:Number = 1024/shortSide
var matrix:Matrix = new Matrix();
if(widthLongest == true)
{
matrix.scale(divideNumLarge, divideNumSmall);
}
else
{
matrix.scale(divideNumSmall, divideNumLarge);
}
var smallBMD:BitmapData = new BitmapData(1024, 1024, false, 0x000000);
smallBMD.draw(evt.target.content.bitmapData, matrix, null, null, null,true);
tempBitmapTexture = new BitmapTexture(smallBMD);
var currModel:Mesh = AssetLibrary.getAsset(currSelectedObj) as Mesh;
TextureMaterial(currModel.material).texture = tempBitmapTexture;
addEventListener(Event.ENTER_FRAME, render);
}
And here is the code for the ios:
function browseTexturesClicked()
{
textureFile = new CameraRoll()
textureFile.browseForImage();
textureFile.addEventListener(MediaEvent.SELECT, onTextureSelected);
}
function onTextureSelected(evt:MediaEvent):void
{
loadPreset = false
var promise:MediaPromise = evt.data as MediaPromise;
textureFile.removeEventListener(MediaEvent.SELECT, onTextureSelected);
mobileTextureLoader = new Loader();
mobileTextureLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, textureLoaded);
mobileTextureLoader.loadFilePromise(promise);
}
function textureLoaded(evt:Event):void
{
removeEventListener(Event.ENTER_FRAME, render);
var bitmapToSave:ByteArray = evt.currentTarget.content.data;
var bitmapFileName:String = "temptextures/" + "texture" + (currSelectedTexture+1).toString() + ".jpg"
var bitmapFile:File = File.applicationStorageDirectory.resolvePath(bitmapFileName);
var bitmapFileStream:FileStream = new FileStream();
bitmapFileStream.open(bitmapFile, FileMode.WRITE);
bitmapFileStream.writeBytes(bitmapToSave, 0, bitmapToSave.length);
bitmapFileStream.close()
currSelectedModelXml.children()[4].children()[currSelectedTexture].children()[1] = "texture" + (currSelectedTexture+1).toString() + ".jpg"
var shortSide:Number
var longSide:Number
var widthLongest:Boolean = true
longSide = evt.target.content.bitmapData.width
shortSide = evt.target.content.bitmapData.height
if(shortSide > longSide)
{
widthLongest = false
shortSide = evt.target.content.bitmapData.width
longSide = evt.target.content.bitmapData.height
}
var divideNumLarge:Number = 1024/longSide
var divideNumSmall:Number = 1024/shortSide
var matrix:Matrix = new Matrix();
if(widthLongest == true)
{
matrix.scale(divideNumLarge, divideNumSmall);
}
else
{
matrix.scale(divideNumSmall, divideNumLarge);
}
var smallBMD:BitmapData = new BitmapData(1024, 1024, false, 0x000000);
smallBMD.draw(evt.target.content.bitmapData, matrix, null, null, null,true);
tempBitmapTexture = new BitmapTexture(smallBMD);
var currModel:Mesh = AssetLibrary.getAsset(currSelectedObj) as Mesh;
TextureMaterial(currModel.material).texture = tempBitmapTexture;
addEventListener(Event.ENTER_FRAME, render);
}