在CMIconManager.m
我有一个名为getImageForIcon
的函数。
- (UIImage *)getImageForIcon:(CMIcon)icon fontSize:(CGFloat)fontSize fontColor:(UIColor *)fontColor imageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight backgroundCircleColor:(UIColor *)backgroundCircleColor backgroundCircleSize:(CGFloat)backgroundCircleSize {
// set up the squares we will be drawing in...
CGSize imageSize = CGSizeMake(imageWidth, imageHeight);
CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);
CGSize circleSize = CGSizeMake(backgroundCircleSize, backgroundCircleSize);
CGRect circleRect = CGRectMake((imageWidth - backgroundCircleSize) / 2, (imageHeight - backgroundCircleSize) / 2, circleSize.width, circleSize.height);
// start the drawing context...
UIGraphicsBeginImageContextWithOptions(imageSize, NO, [[UIScreen mainScreen] scale]);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// fill the image background with transparent alpha...
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -imageRect.size.height);
CGContextSetAlpha(ctx, 0.0);
CGContextFillRect(ctx, CGRectMake(0, 0, imageSize.width, imageSize.height));
// reset subsequent drawing to non-transparent...
CGContextSetAlpha(ctx, 1.0);
// fill in a solid background circle, centered, if needed...
if (![backgroundCircleColor isEqual:[UIColor clearColor]] || !backgroundCircleColor) {
CGContextSetLineWidth(ctx, 1.0);
CGContextSetFillColorWithColor(ctx, backgroundCircleColor.CGColor);
CGContextFillEllipseInRect(ctx, circleRect);
}
// draw the icon, centered in the image...
if ([fontColor isEqual:[UIColor clearColor]]) {
CGContextSetBlendMode(ctx, kCGBlendModeClear);
}
NSAttributedString *theString = [self attributedStringForIcon:icon fontSize:fontSize color:fontColor];
if (theString.length >= 1) {
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) (theString));
CGFloat widthConstraint = imageWidth;
CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(frameSetter, CFRangeMake(0, theString.length), NULL, CGSizeMake(widthConstraint, CGFLOAT_MAX), NULL);
CGFloat distanceFromTop = (imageWidth - suggestedSize.height) / 2;
CGFloat distanceFromLeft = (imageHeight - suggestedSize.width) / 2;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(distanceFromLeft, distanceFromTop, suggestedSize.width, suggestedSize.height));
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
CTFrameDraw(frame, ctx);
CFRelease(frame);
CFRelease(path);
CFRelease(frameSetter);
}
// save the current context in a UIImage object...
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
如何将此功能传递给新文件中的RCT_EXPORT_METHOD()
:RNIconManager.m
?
#import "RNIconManager.h"
#import "CMIconManager.h"
@implementation RNIconManager
RCT_EXPORT_MODULE();
CMIconManager *getIcons = [[CMIconManager alloc] init];
RCT_EXPORT_METHOD(getImageForIcon:(UIImage*)icon
fontSize:(CGFloat)fontSize
fontColor:(UIColor *)fontColor
imageWidth:(CGFloat)imageWidth
imageHeight:(CGFloat)imageHeight
backgroundCircleColor:(UIColor *)backgroundCircleColor
backgroundCircleSize:(CGFloat)backgroundCircleSize
callback:(RCTResponseSenderBlock) callback)
{
callback(@[getIcons(icon, fontSize, fontColor,imageWidth, imageHeight, backgroundCircleColor, backgroundCircleSize)]);
}
答案 0 :(得分:0)
RCT_EXPORT_METHOD
宏将整个方法签名作为参数,因此它看起来像这样:
RCT_EXPORT_METHOD(getImageForIcon:(UIImage*)icon
fontSize:(CGFloat)fontSize
fontColor:(UIColor *)fontColor
imageWidth:(CGFloat)imageWidth
imageHeight:(CGFloat)imageHeight
backgroundCircleColor:(UIColor *)backgroundCircleColor
backgroundCircleSize:(CGFloat)backgroundCircleSize)
{
//your function content goes here
}
一些注意事项:
CMIcon
。您应该查看Native Modules的文档,它包含您需要的所有信息和示例。
修改强> 这是一个如何公开可以静态调用的类方法的示例:
//in your h file
@interface CMIconManager : NSObject
+ (UIImage*)getImageForIcon:(UIImage*)icon fontSize:(CGFloat)fontSize fontColor:(UIColor *)fontColor imageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight backgroundCircleColor:(UIColor *)backgroundCircleColor backgroundCircleSize:(CGFloat)backgroundCircleSize callback:(RCTResponseSenderBlock) callback;
@end
//in your m file
@implementation CMIconManager
+ (void)getImageForIcon:(UIImage*)icon fontSize:(CGFloat)fontSize fontColor:(UIColor *)fontColor imageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight backgroundCircleColor:(UIColor *)backgroundCircleColor backgroundCircleSize:(CGFloat)backgroundCircleSize callback:(RCTResponseSenderBlock) callback
{
//your implementation
}
@end
//call the method
[CMIconManager getImageForIcon:icon fontSize:size fontColor:color imageWidth:width imageHeight:height backgroundCircleColor:backgroundColor backgroundCircleSize:circleSize callback:callback];