forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ASImageNode]fix incorrect backing size calculation (facebookarchive#…
…1189) * fix backing size for image node which content mode is scaleAspectFit * chore: update comments and naming * add change log * Update CHANGELOG.md * Update CHANGELOG.md Co-Authored-By: junjielu <348649634@qq.com> * add unit test for backing size calculation * correct license
- Loading branch information
1 parent
fe1cb1c
commit 8231599
Showing
3 changed files
with
109 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// ASImageNodeBackingSizeTests.mm | ||
// Texture | ||
// | ||
// Copyright (c) Pinterest, Inc. All rights reserved. | ||
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
#import <XCTest/XCTest.h> | ||
#import <AsyncDisplayKit/ASImageNode+CGExtras.h> | ||
|
||
static CGSize _FitSizeWithAspectRatio(CGFloat imageRatio, CGSize backingSize); | ||
static CGSize _FillSizeWithAspectRatio(CGFloat imageRatio, CGSize backingSize); | ||
|
||
static CGSize _FitSizeWithAspectRatio(CGFloat imageRatio, CGSize backingSize) | ||
{ | ||
CGFloat backingRatio = backingSize.width / backingSize.height; | ||
// fit size should be constrained in backing size | ||
if (imageRatio > backingRatio) { | ||
return CGSizeMake(backingSize.width, backingSize.width / imageRatio); | ||
} else { | ||
return CGSizeMake(backingSize.height * imageRatio, backingSize.height); | ||
} | ||
} | ||
|
||
static CGSize _FillSizeWithAspectRatio(CGFloat imageRatio, CGSize backingSize) | ||
{ | ||
CGFloat backingRatio = backingSize.width / backingSize.height; | ||
// backing size should be constrained in fill size | ||
if (imageRatio > backingRatio) { | ||
return CGSizeMake(backingSize.height * imageRatio, backingSize.height); | ||
} else { | ||
return CGSizeMake(backingSize.width, backingSize.width / imageRatio); | ||
} | ||
} | ||
|
||
@interface ASImageNodeBackingSizeTests : XCTestCase | ||
|
||
@end | ||
|
||
@implementation ASImageNodeBackingSizeTests | ||
|
||
- (void)setUp { | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
- (void)tearDown { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
} | ||
|
||
// ScaleAspectFit mode calculation. | ||
- (void)testScaleAspectFitModeBackingSizeCalculation { | ||
CGSize imageSize = CGSizeMake(100, 100); | ||
CGSize boundsSize = CGSizeMake(200, 400); | ||
|
||
CGSize backingSize = CGSizeZero; | ||
CGRect imageDrawRect = CGRectZero; | ||
|
||
ASCroppedImageBackingSizeAndDrawRectInBounds(imageSize, boundsSize, UIViewContentModeScaleAspectFit, CGRectZero, false, CGSizeZero, &backingSize, &imageDrawRect); | ||
CGSize backingSizeShouldBe = _FillSizeWithAspectRatio(boundsSize.width / boundsSize.height, imageSize); | ||
CGSize drawRectSizeShouldBe = _FitSizeWithAspectRatio(imageSize.width / imageSize.height, backingSizeShouldBe); | ||
XCTAssertTrue(CGSizeEqualToSize(backingSizeShouldBe, backingSize)); | ||
XCTAssertTrue(CGSizeEqualToSize(drawRectSizeShouldBe, imageDrawRect.size)); | ||
} | ||
|
||
// ScaleAspectFill mode calculation. | ||
- (void)testScaleAspectFillModeBackingSizeCalculation { | ||
CGSize imageSize = CGSizeMake(100, 100); | ||
CGSize boundsSize = CGSizeMake(200, 400); | ||
|
||
CGSize backingSize = CGSizeZero; | ||
CGRect imageDrawRect = CGRectZero; | ||
|
||
ASCroppedImageBackingSizeAndDrawRectInBounds(imageSize, boundsSize, UIViewContentModeScaleAspectFill, CGRectZero, false, CGSizeZero, &backingSize, &imageDrawRect); | ||
CGSize backingSizeShouldBe = _FitSizeWithAspectRatio(boundsSize.width / boundsSize.height, imageSize); | ||
CGSize drawRectSizeShouldBe = _FillSizeWithAspectRatio(imageSize.width / imageSize.height, backingSizeShouldBe); | ||
XCTAssertTrue(CGSizeEqualToSize(backingSizeShouldBe, backingSize)); | ||
XCTAssertTrue(CGSizeEqualToSize(drawRectSizeShouldBe, imageDrawRect.size)); | ||
} | ||
|
||
@end |