1

我有一个非常简单的应用程序,用户可以从 iOS 照片库中选择一张图片。

传递给事件的 TIBlobTitanium.Media.openPhotoGallery.success然后传递给应用程序级事件。

问题是当收到应用程序级事件时,TIBlob 为 NULL。

下面是一个完整的代码示例。

Titanium.UI.setBackgroundColor('#000');

var win = Ti.UI.createWindow({title: 'Camera Test', exitOnClose: true, fullscreen: true, backgroundColor: '#ffffff'});

var bt = Ti.UI.createButton({'title': 'Gallery', top: 10, width: 200, height: 50});
bt.addEventListener('click', function(e) {
    Titanium.Media.openPhotoGallery({
            success:function(event) {   
                if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
                    alert(event.media);
                    Ti.App.fireEvent('uploadImage', {image: event.media, source: 'gallery'});
                }else {
                    alert('Image was not uploaded because the type was invalid.');
                }
            },
            cancel:function() {
            },
            error:function(err) {
                alert('Error selecting image from gallery: ' + err);
                Ti.API.error(err);
            },
            allowEditing: false,
            autohide: true,
            mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
      });
});

Ti.App.addEventListener('uploadImage', function(e) {
    alert(e.image);
    alert(e.source);
});

win.add(bt);
win.open();

有什么建议么?

4

1 回答 1

3

Appcelerator 指南说,通过的对象和事件必须是 JSON 可序列化的https://wiki.appcelerator.org/display/guides/Event+Handling#EventHandling-Firingevents。TiBlob 是不可序列化的,所以我认为博客没有通过事件。

如果这真的是一个非常简单的应用程序,我建议将其更改为函数调用而不是触发事件,并且 blob 将被保留。但是,如果这绝对需要成为一个事件,您可以event.media.nativePath改为传递,然后在您实际需要对其执行某些操作时从中读取一个 blob。

于 2012-04-30T16:40:53.543 回答