通过应用程序级事件传递的 TIBlob 在接收端变为 NULL

TIBlob passed through application-level event becomes NULL on the receiving end

本文关键字:TIBlob 接收端 NULL 应用程序 事件      更新时间:2023-09-26

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

传递给Titanium.Media.openPhotoGallery.success事件的 TIBlob 随后将传递给应用程序级事件。

问题是,当收到应用程序级别事件时,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();

有什么建议吗?

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

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