Skip to content

Commit e696d4f

Browse files
committed
+ FileAPI.Image.fromDataURL
1 parent 1a4da9f commit e696d4f

3 files changed

Lines changed: 43 additions & 9 deletions

File tree

lib/FileAPI.Flash.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,23 @@
459459
});
460460

461461

462+
api.Image && _inherit(api.Image, {
463+
fromDataURL: function (dataURL, size, callback){
464+
if( !api.support.dataURI || dataURL.length > 3e4 ){
465+
_makeFlashImage(
466+
api.extend({ scale: 'exactFit' }, size)
467+
, dataURL.replace(/^data:[^,]+,/, '')
468+
, function (err, el){ callback(el); }
469+
);
470+
}
471+
else {
472+
this.parent(dataURL, size, callback);
473+
}
474+
}
475+
});
476+
477+
478+
462479
// FileAPI.Camera:statics
463480
api.Camera.fallback = function (el, options, callback){
464481
var camId = api.uid();
@@ -720,11 +737,16 @@
720737

721738

722739
function _makeFlashImage(opts, base64, fn){
723-
var _id, flashId = api.uid(), el = document.createElement('div');
740+
var
741+
key
742+
, flashId = api.uid()
743+
, el = document.createElement('div')
744+
// , attempts = 10
745+
;
724746

725-
for( _id in opts ){
726-
el.setAttribute(_id, opts[_id]);
727-
el[_id] = opts[_id];
747+
for( key in opts ){
748+
el.setAttribute(key, opts[key]);
749+
el[key] = opts[key];
728750
}
729751

730752
_css(el, opts);
@@ -736,9 +758,9 @@
736758
id: flashId
737759
, src: _getUrl(api.flashImageUrl, 'r='+ api.uid())
738760
, wmode: 'opaque'
739-
, flashvars: 'scale='+ opts.scale +'&callback='+_wrap(function _setData(){
740-
_unwrap(_setData);
741-
setTimeout(_setImage, 99);
761+
, flashvars: 'scale='+ opts.scale +'&callback='+_wrap(function _(){
762+
_unwrap(_);
763+
_setImage();
742764
return true;
743765
})
744766
}, opts));

lib/FileAPI.Image.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,18 @@
378378
});
379379

380380

381+
/**
382+
* Create image from DataURL
383+
* @param {String} dataURL
384+
* @param {Object} size
385+
* @param {Function} callback
386+
*/
387+
Image.fromDataURL = function (dataURL, size, callback){
388+
var img = api.newImage(dataURL);
389+
api.extend(img, size);
390+
callback(img);
391+
};
392+
381393

382394
// @export
383395
api.support.canvas = api.support.transform = support;

lib/FileAPI.core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,10 +1272,10 @@
12721272
, type: xhr.getResponseHeader('Content-Type')
12731273
};
12741274
file.dataURL = 'data:'+file.type+';base64,' + api.encode64(xhr.responseBody || xhr.responseText);
1275-
fn({ type: 'load', result: file });
1275+
fn({ type: 'load', result: file }, xhr);
12761276
}
12771277
else {
1278-
fn({ type: 'error' });
1278+
fn({ type: 'error' }, xhr);
12791279
}
12801280
}
12811281
};

0 commit comments

Comments
 (0)