Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-3268] Android: View.toImage() Platform Inconsistency

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionInvalid
Resolution Date2016-09-28T12:08:52.000+0000
Affected Version/sRelease 1.7.0, Release 3.0.0
Fix Version/sn/a
ComponentsAndroid
Labelsapi, parity
ReporterDawson Toth
AssigneeGary Mathews
Created2011-04-15T03:40:51.000+0000
Updated2017-03-24T17:58:25.000+0000

Description

Problem Description

iOS and Android return different objects from the generic "toImage()" function you can call on any view. iOS returns a blob, but Android returns a dictionary (with width, height, x, y, cropRect and media). So if you want to save the result to a file, you need to have different code for the two platforms.

Solution

I don't presume to know the solution to this one, but if the result could be coerced to a blob when it is used as one, that would allow for the same code to work on both platforms. This seems like a sticky situation because existing code will be broken on either platform, depending on the route taken.

Sample Code

The following sample code works, but it requires conditional code for the platform: ".write(Ti.Android ? image.media ? image)".

var win = Ti.UI.createWindow({ backgroundColor: '#fff' });

var web = Ti.UI.createWebView({
    url: 'http://www.google.com/'
});
web.addEventListener('load', function() {
    var image = web.toImage();
    i.Filesystem
            .getFile(Titanium.Filesystem.applicationDataDirectory, 'snapshot_blob.png')
            .write(Ti.Android ? image.media : image);
    alert('PASS: Image saved.');
});
win.add(web);

win.open();

Associated API Docs

The API Docs say toImage returns a blob. http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.View.toImage-method.html

Tested On

Titanium SDK version: 1.7.0 (03/03/11 10:45 87a2113...) iPhone Simulator 4.2 Android Emulator 2.2 APIs Android Device Epic 4G 2.2

Associated Helpdesk Ticket

http://developer.appcelerator.com/helpdesk/view/76200

Comments

  1. Sasa Savic 2012-01-27

    Any updates on this issue?
  2. Junaid Younus 2012-09-04

    Tested on a Samsung Galaxy S2 and the iOS simulator using TiSDK 2.2.0v20120904101713, issue still valid.
  3. Lokesh Choudhary 2014-03-04

    Verified the issue is not reproducible. The view.toImage() successfully converts the webview to image in the above test code. Environment: Appc Studio : 3.2.1.201402061120 Ti SDK : 3.2.1.GA Mac OSX : 10.8.5 Alloy : 1.3.1 CLI - 3.2.1 Nexus 5 - android 4.4.2 Samsung S4 - android 4.2.2 Samsung S3 - android 4.0.4
  4. Fokke Zandbergen 2014-11-14

    I can reproduce this on 3.4.1.RC

    Test Case

       var b = Ti.UI.createView().toImage();
       
       console.log(typeof b);
       console.log(b.apiName);
       console.log(b);
       

    Android

       object
       undefined
       {
         "cropRect": {
           "height": 318,
           "width": 318,
           "x": 0
         },
         "height": 318,
         "media": {
           "height": 318,
           "bubbleParent": true,
           "type": 2,
           "mimeType": "image/png",
           "apiName": "Ti.Blob",
           "nativePath": null,
           "file": null,
           "text": null,
           "length": 29062,
           "width": 318
         },
         "mimeType": "image/png",
         "y": 0,
         "width": 318,
         "x": 0
       }
       

    iOS:

       object
       Ti.Blob
       {}
       
  5. Rainer Schleevoigt 2015-08-18

    I have the same issue. I tried to use [http://www.batasoft.com/appcelerator/app-icons](http://www.batasoft.com/appcelerator/app-icons) and this lib uses toImage() to convert a label to a image for using in tabgroup.
  6. Gary Mathews 2016-09-28

    This issue was fixed by https://github.com/appcelerator/titanium_mobile/pull/6907
       var b = Ti.UI.createView().toImage();
       Ti.API.info(typeof b);
       Ti.API.info(b.apiName);
       Ti.API.info(b);
       
       object
       Ti.Blob
       [object TiBlob]
       
    Resolved as invalid.
  7. Lee Morris 2017-03-24

    Closing ticket as invalid with reference to the above comments.

JSON Source