Skip to content

Commit

Permalink
Add size property to RiveImageAsset on iOS
Browse files Browse the repository at this point in the history
Similar to #8167:

> Request from a customer.
>
> This unlocks the ability to resize images at runtime to fit the appropriate ImageAsset dimensions.

Rather than exposing an individual width and height, `CGSize` is used (similar to `UIImage`), accessible via a new `size` property on `RiveImageAsset`. A unit test was also added that tests a very simple referenced jpeg of size 320x240.

Diffs=
48212b2bb Add size property to RiveImageAsset on iOS (#8169)

Co-authored-by: David Skuza <[email protected]>
  • Loading branch information
dskuza and dskuza committed Sep 19, 2024
1 parent fee1c93 commit 6a8244e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
adc2405543ea447b83fda212a0cd7cc4eafd4de9
48212b2bbd1f55028502f0669ea2be69c7a62694
6 changes: 5 additions & 1 deletion RiveRuntime.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
F21F08142C66526D00FFA205 /* RiveFallbackFontDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = F21F08132C66526D00FFA205 /* RiveFallbackFontDescriptor.swift */; };
F23626AA2C8F90FA00727D9A /* nested_text_run.riv in Resources */ = {isa = PBXBuildFile; fileRef = F23626A92C8F90FA00727D9A /* nested_text_run.riv */; };
F28DE4532C5002D900F3C379 /* RiveModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F28DE4522C5002D900F3C379 /* RiveModelTests.swift */; };
F2CCA9792C9B2799007DC0D2 /* referenced_image_asset.riv in Resources */ = {isa = PBXBuildFile; fileRef = F2CCA9782C9B2799007DC0D2 /* referenced_image_asset.riv */; };
F2D285492C6D469900728340 /* RiveFallbackFontProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = F2D285482C6D469900728340 /* RiveFallbackFontProvider.swift */; };
F2ECC2312C666824008B20E5 /* RiveFallbackFontDescriptor+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F2ECC2302C666824008B20E5 /* RiveFallbackFontDescriptor+Extensions.swift */; };
F2ECC23A2C66B949008B20E5 /* RiveFontTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F2ECC2382C66B920008B20E5 /* RiveFontTests.swift */; };
Expand Down Expand Up @@ -203,6 +204,7 @@
F21F08132C66526D00FFA205 /* RiveFallbackFontDescriptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveFallbackFontDescriptor.swift; sourceTree = "<group>"; };
F23626A92C8F90FA00727D9A /* nested_text_run.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = nested_text_run.riv; sourceTree = "<group>"; };
F28DE4522C5002D900F3C379 /* RiveModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveModelTests.swift; sourceTree = "<group>"; };
F2CCA9782C9B2799007DC0D2 /* referenced_image_asset.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = referenced_image_asset.riv; sourceTree = "<group>"; };
F2D285482C6D469900728340 /* RiveFallbackFontProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveFallbackFontProvider.swift; sourceTree = "<group>"; };
F2ECC2302C666824008B20E5 /* RiveFallbackFontDescriptor+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RiveFallbackFontDescriptor+Extensions.swift"; sourceTree = "<group>"; };
F2ECC2382C66B920008B20E5 /* RiveFontTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveFontTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -262,6 +264,7 @@
04BE53F726493FE600427B39 /* Assets */ = {
isa = PBXGroup;
children = (
F2CCA9782C9B2799007DC0D2 /* referenced_image_asset.riv */,
F23626A92C8F90FA00727D9A /* nested_text_run.riv */,
0424A8752BD59435000A9A1C /* img_test.riv */,
04E2223F2BE3C85100D82668 /* ball_test.riv */,
Expand Down Expand Up @@ -554,6 +557,7 @@
041265282B0CC387009400EC /* hosted_assets.riv in Resources */,
E57798AB2A7310B700FF25C3 /* testtext.riv in Resources */,
04ED72F3299C115100E8DE53 /* empty_animation_state.riv in Resources */,
F2CCA9792C9B2799007DC0D2 /* referenced_image_asset.riv in Resources */,
E599DCFA2AAFA06100D1E49A /* rating_animation.riv in Resources */,
04BE54062649403600427B39 /* off_road_car_blog.riv in Resources */,
04BE540C2649403600427B39 /* multipleartboards.riv in Resources */,
Expand Down Expand Up @@ -583,7 +587,7 @@
043026042AFBA04100320F2E /* RiveFactory.mm in Sources */,
04BE5434264D267900427B39 /* LayerState.mm in Sources */,
C9601F2B250C25930032AA07 /* CoreGraphicsRenderer.mm in Sources */,
C9601F2B250C25930032AA07 /* RiveRenderer.mm in Sources */,
C9601F2B250C25930032AA07 /* CoreGraphicsRenderer.mm in Sources */,
043025F42AF90EAC00320F2E /* RiveFileAssetLoader.mm in Sources */,
F2D285492C6D469900728340 /* RiveFallbackFontProvider.swift in Sources */,
043025FC2AFA862E00320F2E /* FileAssetLoaderAdapter.mm in Sources */,
Expand Down
6 changes: 6 additions & 0 deletions Source/Renderer/RiveFileAsset.mm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ - (instancetype)initWithFileAsset:(const rive::ImageAsset*)fileAsset
return [super initWithFileAsset:fileAsset];
}

- (CGSize)size
{
auto asset = (rive::ImageAsset*)[self getInstance];
return CGSizeMake(asset->width(), asset->height());
}

- (void)renderImage:(RiveRenderImage*)image
{

Expand Down
1 change: 1 addition & 0 deletions Source/Renderer/include/RiveFileAsset.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ NS_ASSUME_NONNULL_BEGIN
* RiveImageAsset
*/
@interface RiveImageAsset : RiveFileAsset
@property(nonatomic, readonly) CGSize size;
- (void)renderImage:(RiveRenderImage*)image;
@end

Expand Down
Binary file added Tests/Assets/referenced_image_asset.riv
Binary file not shown.
22 changes: 22 additions & 0 deletions Tests/OutOfBandAssetTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,26 @@ - (void)testEmbeddedAssetsProvideData
XCTAssertEqualObjects([font cdnUuid], @"");
}

- (void)testReferencedImageAssetReturnsSize
{
NSError* error = nil;
NSData* data = [Util loadTestData:@"referenced_image_asset"];
__block RiveImageAsset* image;

RiveFile* file = [[RiveFile alloc]
initWithData:data
loadCdn:false
customAssetLoader:^bool(RiveFileAsset* asset, NSData* data, RiveFactory* factory) {
if ([asset isKindOfClass:[RiveImageAsset class]])
{
image = (RiveImageAsset*)asset;
}
return false;
}
error:&error];

XCTAssertEqual(image.size.width, 320);
XCTAssertEqual(image.size.height, 240);
}

@end

0 comments on commit 6a8244e

Please sign in to comment.