React Native Bridge - 在Objective-C中的另一个文件中导出一个函数

时间:2017-04-21 22:31:44

标签: ios objective-c react-native react-native-ios

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)]);
}

1 个答案:

答案 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
}

一些注意事项:

  1. 您不能在方法中使用任何类型。您只能使用RN知道如何转换的类型,因此您无法使用CMIcon
  2. 您不能简单地从导出的方法返回值。如果您需要返回某些内容,则需要通过回调或使用承诺。
  3. 如果你想要调用的实际函数在另一个类中找到,你仍然需要像我提到的那样进行导出,但是在这个导出的方法中你可以调用另一个方法 - 它可以是静态方法或者根据代码编写方式的实例方法。
  4. 您应该查看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];