From 141b02b142018d7b10508d8dc5e788fd154f0a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Mon, 10 Jan 2022 11:58:16 +0800 Subject: [PATCH] add resource loader & image decoder plugin --- doric-iOS/Pod/Classes/DoricRegistry.m | 4 + .../Classes/Plugin/DoricImageDecoderPlugin.h | 24 ++++++ .../Classes/Plugin/DoricImageDecoderPlugin.m | 74 +++++++++++++++++++ .../Plugin/DoricResourceLoaderPlugin.h | 24 ++++++ .../Plugin/DoricResourceLoaderPlugin.m | 40 ++++++++++ 5 files changed, 166 insertions(+) create mode 100644 doric-iOS/Pod/Classes/Plugin/DoricImageDecoderPlugin.h create mode 100644 doric-iOS/Pod/Classes/Plugin/DoricImageDecoderPlugin.m create mode 100644 doric-iOS/Pod/Classes/Plugin/DoricResourceLoaderPlugin.h create mode 100644 doric-iOS/Pod/Classes/Plugin/DoricResourceLoaderPlugin.m diff --git a/doric-iOS/Pod/Classes/DoricRegistry.m b/doric-iOS/Pod/Classes/DoricRegistry.m index 29ce9fe4..84163d6b 100644 --- a/doric-iOS/Pod/Classes/DoricRegistry.m +++ b/doric-iOS/Pod/Classes/DoricRegistry.m @@ -42,6 +42,8 @@ #import "DoricFlowLayoutNode.h" #import "DoricPopoverPlugin.h" #import "DoricAnimatePlugin.h" +#import "DoricResourceLoaderPlugin.h" +#import "DoricImageDecoderPlugin.h" #import "DoricNestedSliderNode.h" #import "DoricInputNode.h" #import "DoricDraggableNode.h" @@ -116,6 +118,8 @@ - (void)innerRegister { [self registerNativePlugin:DoricStatusBarPlugin.class withName:@"statusbar"]; [self registerNativePlugin:DoricCoordinatorPlugin.class withName:@"coordinator"]; [self registerNativePlugin:DoricNotchPlugin.class withName:@"notch"]; + [self registerNativePlugin:DoricResourceLoaderPlugin.class withName:@"resourceLoader"]; + [self registerNativePlugin:DoricImageDecoderPlugin.class withName:@"imageDecoder"]; [self registerViewNode:DoricStackNode.class withName:@"Stack"]; [self registerViewNode:DoricVLayoutNode.class withName:@"VLayout"]; diff --git a/doric-iOS/Pod/Classes/Plugin/DoricImageDecoderPlugin.h b/doric-iOS/Pod/Classes/Plugin/DoricImageDecoderPlugin.h new file mode 100644 index 00000000..7d2f31e9 --- /dev/null +++ b/doric-iOS/Pod/Classes/Plugin/DoricImageDecoderPlugin.h @@ -0,0 +1,24 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by jingpeng.wang on 2022/1/10. +// + +#import +#import "DoricNativePlugin.h" + +@interface DoricImageDecoderPlugin : DoricNativePlugin +@end diff --git a/doric-iOS/Pod/Classes/Plugin/DoricImageDecoderPlugin.m b/doric-iOS/Pod/Classes/Plugin/DoricImageDecoderPlugin.m new file mode 100644 index 00000000..77cf55da --- /dev/null +++ b/doric-iOS/Pod/Classes/Plugin/DoricImageDecoderPlugin.m @@ -0,0 +1,74 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by jingpeng.wang on 2022/1/10. +// + +#import +#import "DoricImageDecoderPlugin.h" +#import "DoricExtensions.h" + +@implementation DoricImageDecoderPlugin +- (void)getImageInfo:(NSDictionary *)resource withPromise:(DoricPromise *)promise { + DoricResource *doricResource = [self.doricContext.driver.registry.loaderManager load:resource withContext:self.doricContext]; + if (doricResource != nil) { + DoricAsyncResult *asyncResult = [doricResource fetch]; + [asyncResult setResultCallback:^(id _Nonnull result) { + UIImage *image = [UIImage imageWithData:result]; + [promise resolve:@{ + @"width": @(image.size.width), + @"height": @(image.size.height) + }]; + }]; + } else { + DoricLog(@"Cannot find loader for resource %s", resource.description); + [promise reject:@"Load error"]; + } +} + +- (void)decodeToPixels:(NSDictionary *)resource withPromise:(DoricPromise *)promise { + DoricResource *doricResource = [self.doricContext.driver.registry.loaderManager load:resource withContext:self.doricContext]; + if (doricResource != nil) { + DoricAsyncResult *asyncResult = [doricResource fetch]; + [asyncResult setResultCallback:^(id _Nonnull result) { + UIImage *image = [UIImage imageWithData:result]; + + CGImageRef imageRef = image.CGImage; + NSUInteger iWidth = CGImageGetWidth(imageRef); + NSUInteger iHeight = CGImageGetHeight(imageRef); + NSUInteger iBytesPerPixel = 4; + NSUInteger iBytesPerRow = iBytesPerPixel * iWidth; + NSUInteger iBitsPerComponent = 8; + unsigned char *imageBytes = malloc(iWidth * iHeight * iBytesPerPixel); + + CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); + + CGContextRef context = CGBitmapContextCreate(imageBytes, iWidth, iHeight, iBitsPerComponent, iBytesPerRow, colorspace, kCGImageAlphaPremultipliedLast); + CGRect rect = CGRectMake(0, 0, iWidth, iHeight); + CGContextDrawImage(context, rect, imageRef); + CGColorSpaceRelease(colorspace); + CGContextRelease(context); + + NSData* data = [NSData dataWithBytes:(const void *)imageBytes length:sizeof(unsigned char) * iWidth * iHeight * iBytesPerPixel]; + + [promise resolve:data]; + }]; + } else { + DoricLog(@"Cannot find loader for resource %s", resource.description); + [promise reject:@"Load error"]; + } +} +@end diff --git a/doric-iOS/Pod/Classes/Plugin/DoricResourceLoaderPlugin.h b/doric-iOS/Pod/Classes/Plugin/DoricResourceLoaderPlugin.h new file mode 100644 index 00000000..a03912f9 --- /dev/null +++ b/doric-iOS/Pod/Classes/Plugin/DoricResourceLoaderPlugin.h @@ -0,0 +1,24 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by jingpeng.wang on 2022/1/10. +// + +#import +#import "DoricNativePlugin.h" + +@interface DoricResourceLoaderPlugin : DoricNativePlugin +@end diff --git a/doric-iOS/Pod/Classes/Plugin/DoricResourceLoaderPlugin.m b/doric-iOS/Pod/Classes/Plugin/DoricResourceLoaderPlugin.m new file mode 100644 index 00000000..c4cf5f02 --- /dev/null +++ b/doric-iOS/Pod/Classes/Plugin/DoricResourceLoaderPlugin.m @@ -0,0 +1,40 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// +// Created by jingpeng.wang on 2022/1/10. +// + +#import +#import "DoricResourceLoaderPlugin.h" +#import "DoricExtensions.h" + +@implementation DoricResourceLoaderPlugin +- (void)load:(NSDictionary *)resource withPromise:(DoricPromise *)promise { + DoricResource *doricResource = [self.doricContext.driver.registry.loaderManager load:resource withContext:self.doricContext]; + if (doricResource != nil) { + DoricAsyncResult *asyncResult = [doricResource fetch]; + [asyncResult setResultCallback:^(id _Nonnull result) { + [promise resolve:result]; + }]; + [asyncResult setExceptionCallback:^(NSException * _Nonnull e) { + DoricLog(@"Cannot load resource %s, %s", resource.description, e.description); + }]; + } else { + DoricLog(@"Cannot find loader for resource %s", resource.description); + [promise reject:@"Load error"]; + } +} +@end