当手机中不存在图像时,用联系人照片替换默认图像 [英] replace default image with contact photo when image doesn't exist in phonegap

查看:188
本文介绍了当手机中不存在图像时,用联系人照片替换默认图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Android的phonegap获取联系人数据,这里我试图获取联系人图像,并尝试替换为默认图像,如果实际的联系人照片不存在。

I am trying to fetch contact data from phonegap for android , here I am trying to fetch the contact image and trying to replace it with the default image if the actual contact photo doesnt exist.

现在在phonegap当我们尝试访问照片,它返回的url,即使照片不存在。所以我在这里尝试创建 Image 对象并分配从phonegap获取的照片url如果图像不存在,图像 object将引发 onerror 异常,其中我将其 src 属性更改为默认图像。

now in phonegap when we try to access photo it returns the url even if the photo doesn't exist. so what I am trying here to create Image object and assigning the photo url fetched from phonegap if the image doesnt exist , Image object will raise onerror exception and inside that I am changing its src attribute to default image.

但现在的问题是 onerror 函数触发后面我的意思是在数据加载后, src 属性不会改变。因此任何人都可以告诉我如何管理 onerror

but now the problem is onerror functions fires late I mean after the data loads so src attribute is not getting changed. so can any one tell me how do I mange onerror

   if(contacts[i].photos){  
                        for(k =0 ;k < contacts[i].photos.length;k++){



                            var imgVal = new Image();
                            imgVal.onerror = function(){


                          this.src= 'resources/images/default_usr.png';
                          this.error = null;

                                contactData.setContactImage(this.outerHTML);
                                console.log("2");
                            }
                            imgVal.width="45";
                            imgVal.height="45";
                            imgVal.src = contacts[i].photos[k].value;

                            contactData.setContactImage(imgVal.src);


                            console.log("1");       

                        }//end for contact photo
                    }// end if contact photo


推荐答案

您没有显示如何创建contactData,我自己没有这样做,但这里有一些注意事项从阅读您的代码,假设当前您的调试控制台显示

You didn't show how you create contactData and I haven't done this myself, but here are a couple of considerations from reading your code, assuming that currently your debug console shows

1
2


  • 在主循环中,您调用

    b $ b contactData.setContactImage( imgVal.src );并以 image src 属性作为参数(图片网址);在错误循环中,您分配 this.src ,希望它与imgVal对象相同,然后将对象的 outerHTML 属性传递给setContactImage。这是非常不同的,为了一致性,你应该传递this.src作为参数,我找不到官方文档,但从几个示例,我发现参数应该是完整的Image对象。

  • In the main loop, you invoke contactData.setContactImage(imgVal.src); with the image src property as a parameter (the image url); In the error loop, you assign this.src which hopefully is the same imgVal object and then pass the object's outerHTML property to the setContactImage. This is very different, for consistency you should pass this.src as an argument and I couldn't find the official docs, but from a few samples I found the parameter should be the full Image object. Whichever the parameter type, it needs to be consistent across the two calls.

使你的生活变得更容易,javascript使用闭包,即imgVal在onerror中仍然可用事件,所以你可以使用imgVal而不是这个;

to make your life easier, javascript uses closures i.e. imgVal is still available in the onerror event so you could use imgVal instead of this;

尝试console.log(imgVal)在主循环和错误声明你正在调用函数setContactData with);这应该清除一切;

try to console.log(imgVal) in both the main loop and the error declaration (or the exact parameter you are invoking the function setContactData with); this should clear things up;

onerror不能太晚;你正在调用一个将开始自己执行的方法。它可能被禁止在onerror子句中调用它(因为你冒无限的onerror循环) - 这只是一个假设,但我从来没有尝试过;但你仍然可以在onerror函数中创建一个新的Image对象。

onerror cannot be too late; you are invoking a method that will start its own execution. It may be forbidden to invoke it in an onerror clause (because you risk an infinite onerror loop) - this is just a hypothesis though, I never tried; but you might still work around this creating a new Image object in the onerror function.

这篇关于当手机中不存在图像时,用联系人照片替换默认图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆