Skip to content

Commit c2fa9f7

Browse files
authored
fix(android): imageAsResized to squared images (#13723)
* fix(android): imageAsResized to squared images * fix(android): imageAsResized to squared images * Update TiBlob.java
1 parent cf4b370 commit c2fa9f7

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

android/titanium/src/java/org/appcelerator/titanium/TiBlob.java

+5
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,11 @@ public TiBlob imageAsResized(Number width, Number height)
720720
imageResized = Bitmap.createScaledBitmap(img, dstWidth, dstHeight, true);
721721
}
722722
}
723+
724+
if (imageResized.getHeight() != dstHeight || imageResized.getWidth() != dstWidth) {
725+
// image didn't resize - fallback
726+
imageResized = Bitmap.createScaledBitmap(img, dstWidth, dstHeight, true);
727+
}
723728
if (img != image && img != imageResized) {
724729
img.recycle();
725730
img = null;

tests/Resources/Logo_non_square.png

1.78 KB
Loading
1.78 KB
Loading

tests/Resources/ti.blob.test.js

+75
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,88 @@ describe('Titanium.Blob', function () {
309309
should(b.height).be.eql(60);
310310
});
311311

312+
it('with PNG (square)',function(){
313+
constblob=Ti.Filesystem.getFile('Logo.png').read();
314+
constb=blob.imageAsResized(50,50);
315+
should(b).be.an.Object();
316+
should(b.width).be.eql(50);
317+
should(b.height).be.eql(50);
318+
});
319+
320+
it('with non-image (JS file) returns null',function(){
321+
constblob=Ti.Filesystem.getFile('app.js').read();
322+
constb=blob.imageAsResized(50,60);
323+
should.not.exist(b);
324+
});
325+
});
326+
327+
describe('#imageAsResized() - non square',function(){
328+
it('is a Function',function(){
329+
constblob=Ti.Filesystem.getFile('Logo_non_square.png').read();
330+
should(blob.imageAsResized).be.a.Function();
331+
});
332+
333+
it('with PNG',function(){
334+
constblob=Ti.Filesystem.getFile('Logo_non_square.png').read();
335+
constb=blob.imageAsResized(50,60);
336+
should(b).be.an.Object();
337+
should(b.width).be.eql(50);
338+
should(b.height).be.eql(60);
339+
});
340+
341+
it('with PNG (square)',function(){
342+
constblob=Ti.Filesystem.getFile('Logo_non_square.png').read();
343+
constb=blob.imageAsResized(50,50);
344+
should(b).be.an.Object();
345+
should(b.width).be.eql(50);
346+
should(b.height).be.eql(50);
347+
});
348+
312349
it('with non-image (JS file) returns null',function(){
313350
constblob=Ti.Filesystem.getFile('app.js').read();
314351
constb=blob.imageAsResized(50,60);
315352
should.not.exist(b);
316353
});
317354
});
318355

356+
describe('#imageAsResized() from imageview',function(){
357+
it('square to non square',function(){
358+
constimg=Ti.UI.createImageView({image: 'Logo.png'});
359+
constblob=img.toBlob();
360+
constb=blob.imageAsResized(50,60);
361+
should(b).be.an.Object();
362+
should(b.width).be.eql(50);
363+
should(b.height).be.eql(60);
364+
});
365+
366+
it('non square to non square',function(){
367+
constimg=Ti.UI.createImageView({image: 'Logo_non_square.png'});
368+
constblob=img.toBlob();
369+
constb=blob.imageAsResized(50,60);
370+
should(b).be.an.Object();
371+
should(b.width).be.eql(50);
372+
should(b.height).be.eql(60);
373+
});
374+
375+
it('square to square',function(){
376+
constimg=Ti.UI.createImageView({image: 'Logo.png'});
377+
constblob=img.toBlob();
378+
constb=blob.imageAsResized(60,60);
379+
should(b).be.an.Object();
380+
should(b.width).be.eql(60);
381+
should(b.height).be.eql(60);
382+
});
383+
384+
it('non square to square',function(){
385+
constimg=Ti.UI.createImageView({image: 'Logo_non_square.png'});
386+
constblob=img.toBlob();
387+
constb=blob.imageAsResized(60,60);
388+
should(b).be.an.Object();
389+
should(b.width).be.eql(60);
390+
should(b.height).be.eql(60);
391+
});
392+
});
393+
319394
describe('#imageAsThumbnail()',function(){
320395
it('is a Function',function(){
321396
constblob=Ti.Filesystem.getFile('Logo.png').read();

0 commit comments

Comments
 (0)
close