CMIconManager.m
我有一個叫做getImageForIcon
的函數。React Native Bridge - 在Objective-C中導出另一個文件中的一個函數
- (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)]);
}
這在我看來,'getImageForIcon'是實例方法,但你使用這個像類方法。 – Ryan
這很可能。我對Objective C完全陌生,我可能更像JavaScript一樣使用它。目標C中的差異是什麼? – Turnipdabeets
將' - (UIImage *)'更改爲'+(UIImage *)',但沒有有效的方法將數據從類方法發送到實例。我用'NSNotificatio'代替。 – Ryan