2012-07-02 19 views
2

我試圖運行從我在UIWebView中加載的URL返回的JavaScript。該腳本拍攝圖像並在其上繪製一個點以顯示用戶位置。iPhone上的某些移動瀏覽器沒有正確執行Javascript

在Mobile Safari,UIWebView和Google Chrome Mobile中,圖像生成不正確,點位置錯誤。

在桌面Safari,Chrome瀏覽器,Opera Mini和Android版本的我的應用程序,它工作正常。

我需要幫助讓它在UIWebView/Mobile Safari中工作。

問題1:對zoomIn()方法中的context.drawImage()的調用在Mobile Safari的調試控制檯中產生以下錯誤:「Javascript錯誤undefined。INDEX_SIZE_ERR:DOM例外1:索引或大小是負值,或者大於允許值。「

問題2:在zoomOut()方法中,圖像本身繪製得很好,但點不在正確的位置。

UPDATE:從下面的代碼中可以看出,我添加了console.log語句來打印出原始圖像的寬度和高度。在違規瀏覽器這些值是一半他們應該是。我現在試圖找出原因。

代碼:

window.onload = function zoomIn() { 
    var canvas = document.getElementById("area"); 
    var context = canvas.getContext("2d"); 
    var imageObj = new Image(); 
    var px = 1988; 
    var py = 734; 
    imageObj.src = "IMAGE URL GOES HERE"; 
    imageObj.onload = function() { 
     canvas.width=640; 
     canvas.height=480; 
     if(px-canvas.width<0 || py-canvas.height<0){ 
      canvas.width=500; 
      canvas.height=300; 
     } 

     console.log(imageObj.height); 
     console.log(imageObj.width); 

     context.drawImage(imageObj, px-canvas.width, py-canvas.height, canvas.width*2, canvas.height*2,0,0,canvas.width,canvas.height); 


     context.beginPath(); 
     context.arc(canvas.width/2, canvas.height/2, 10, 0, 2 * Math.PI, false); 
     context.fillStyle = "red"; 
     context.fill(); 
     context.lineWidth = 3; 
     context.strokeStyle = "black"; 
     context.beginPath(); 
     context.font = "10pt Calibri"; 
     context.fillStyle = "black" 
     context.textAlign = "center"; 
     context.fillText("You Are Here", canvas.width/2, canvas.height/2+20); 
     context.stroke(); 
    }; 
    document.form1.button2.style.visibility="hidden" 
    document.form1.button1.style.visibility="visible"; 
}; 

function zoomOut(){ 
    var canvas = document.getElementById("area"); 
    var context = canvas.getContext("2d"); 
    var imageObj = new Image(); 
    var px = 1988; 
    var py = 734; 
    imageObj.src = "IMAGE URL GOES HERE"; 
    imageObj.onload = function(){ 
     canvas.width=imageObj.width 
     canvas.height=imageObj.height 
     context.drawImage(imageObj,0,0); 

     context.arc(px, py, 20, 0, 2 * Math.PI, false); 
     context.fillStyle = "red"; 
     context.fill(); 
     context.lineWidth = 3; 
     context.strokeStyle = "black"; 

     context.beginPath(); 
     context.rect(0, 0, canvas.width, canvas.height); 
     context.font = "15pt Calibri"; 
     context.fillStyle = "black"; 
     context.textAlign = "center"; 

     context.fillText("You Are Here",px,py+25); 
     context.stroke(); 
    }; 
    document.form1.button1.style.visibility="hidden" 
    document.form1.button2.style.visibility="visible"; 
} 
+0

你能後的代碼?您是否嘗試過桌面Safari中的代碼? – nhahtdh

+0

在桌面Safari中正常工作。 – Groppe

+0

你會在哪一行發現錯誤? – Bergi

回答

0

如果你讀過我的整個後(不包括代碼),你會知道,我最終發現,在iPhone上的原始圖像的尺寸我們一半他們在其他瀏覽器中的內容。

這是由蘋果公司對可下載的圖像大小(即3百萬像素)的限制引起的。我的形象比那大。但是,JavaScript引用了圖片的某些部分(在iOS上),我們超出了它的範圍。

來源:Apple Documentation: Know iOS Resource Limits

相關問題