add resource loader & image decoder plugin

This commit is contained in:
王劲鹏 2022-01-10 11:58:16 +08:00 committed by osborn
parent e3f45676d6
commit 141b02b142
5 changed files with 166 additions and 0 deletions

View File

@ -42,6 +42,8 @@
#import "DoricFlowLayoutNode.h" #import "DoricFlowLayoutNode.h"
#import "DoricPopoverPlugin.h" #import "DoricPopoverPlugin.h"
#import "DoricAnimatePlugin.h" #import "DoricAnimatePlugin.h"
#import "DoricResourceLoaderPlugin.h"
#import "DoricImageDecoderPlugin.h"
#import "DoricNestedSliderNode.h" #import "DoricNestedSliderNode.h"
#import "DoricInputNode.h" #import "DoricInputNode.h"
#import "DoricDraggableNode.h" #import "DoricDraggableNode.h"
@ -116,6 +118,8 @@ - (void)innerRegister {
[self registerNativePlugin:DoricStatusBarPlugin.class withName:@"statusbar"]; [self registerNativePlugin:DoricStatusBarPlugin.class withName:@"statusbar"];
[self registerNativePlugin:DoricCoordinatorPlugin.class withName:@"coordinator"]; [self registerNativePlugin:DoricCoordinatorPlugin.class withName:@"coordinator"];
[self registerNativePlugin:DoricNotchPlugin.class withName:@"notch"]; [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:DoricStackNode.class withName:@"Stack"];
[self registerViewNode:DoricVLayoutNode.class withName:@"VLayout"]; [self registerViewNode:DoricVLayoutNode.class withName:@"VLayout"];

View File

@ -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 <Foundation/Foundation.h>
#import "DoricNativePlugin.h"
@interface DoricImageDecoderPlugin : DoricNativePlugin
@end

View File

@ -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 <DoricCore/Doric.h>
#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

View File

@ -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 <Foundation/Foundation.h>
#import "DoricNativePlugin.h"
@interface DoricResourceLoaderPlugin : DoricNativePlugin
@end

View File

@ -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 <DoricCore/Doric.h>
#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