尝试从对象 [0] 插入 nil 对象



我的 React Native 应用程序的一个功能包括对文档照片进行透视校正。

它需要 4 个点,裁剪图像,透视校正它,然后应用 CIFilter 调整颜色并导出为 Base64 字符串。

我们试图在iPhone 11模拟器上运行它

截至目前,我们一直收到此错误

attempt to insert nil object from objects[0]

假设:这可能是因为它无法从文件存储中读取图像/文件正在读取nil

这是源代码

#import "CustomCropManager.h"
#import <React/RCTLog.h>
@implementation CustomCropManager
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(crop:(NSDictionary *)points imageUri:(NSString *)imageUri callback:(RCTResponseSenderBlock)callback)
{
NSLog(@"[myLOG] PARSING");
NSString *parsedImageUri = [imageUri stringByReplacingOccurrencesOfString:@"file://" withString:@""];
NSLog(@"[myLOG] parsedImageUri");

NSURL *fileURL = [NSURL fileURLWithPath:parsedImageUri];
NSLog(@"[myLOG] fileURL");
CIImage *ciImage = [CIImage imageWithContentsOfURL:fileURL];
NSLog(@"[myLOG] ciImage");
CGPoint newLeft = CGPointMake([points[@"topLeft"][@"x"] floatValue], [points[@"topLeft"][@"y"] floatValue]);
CGPoint newRight = CGPointMake([points[@"topRight"][@"x"] floatValue], [points[@"topRight"][@"y"] floatValue]);
CGPoint newBottomLeft = CGPointMake([points[@"bottomLeft"][@"x"] floatValue], [points[@"bottomLeft"][@"y"] floatValue]);
CGPoint newBottomRight = CGPointMake([points[@"bottomRight"][@"x"] floatValue], [points[@"bottomRight"][@"y"] floatValue]);
NSLog(@"[myLOG] CGPOINTS");
newLeft = [self cartesianForPoint:newLeft height:[points[@"height"] floatValue] ];
newRight = [self cartesianForPoint:newRight height:[points[@"height"] floatValue] ];
newBottomLeft = [self cartesianForPoint:newBottomLeft height:[points[@"height"] floatValue] ];
newBottomRight = [self cartesianForPoint:newBottomRight height:[points[@"height"] floatValue] ];
NSLog(@"[myLOG] new");

NSMutableDictionary *rectangleCoordinates = [[NSMutableDictionary alloc] init];
rectangleCoordinates[@"inputTopLeft"] = [CIVector vectorWithCGPoint:newLeft];
rectangleCoordinates[@"inputTopRight"] = [CIVector vectorWithCGPoint:newRight];
rectangleCoordinates[@"inputBottomLeft"] = [CIVector vectorWithCGPoint:newBottomLeft];
rectangleCoordinates[@"inputBottomRight"] = [CIVector vectorWithCGPoint:newBottomRight];
NSLog(@"[myLOG] rectangleCoordinates");
ciImage = [ciImage imageByApplyingFilter:@"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates];
NSLog(@"[myLOG] ciImage");
// custom code
CIFilter* colorControlsFilter = [CIFilter filterWithName:@"CIColorControls"];
[colorControlsFilter setValue:ciImage forKey:@"inputImage"];
[colorControlsFilter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputSaturation"];
[colorControlsFilter setValue:[NSNumber numberWithFloat:0.2] forKey:@"inputBrightness"];
[colorControlsFilter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputContrast"];
ciImage = [colorControlsFilter valueForKey:@"outputImage"];
// custom code
NSLog(@"[myLOG] ciImage ssss");
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimage = [context createCGImage:ciImage fromRect:[ciImage extent]];
UIImage *image = [UIImage imageWithCGImage:cgimage];
NSLog(@"[myLOG] image");
NSData *imageToEncode = UIImageJPEGRepresentation(image, 0.8);
NSLog(@"[myLOG] calling...");
callback(@[[NSNull null], @{@"image": [imageToEncode base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]}]);
}
- (CGPoint)cartesianForPoint:(CGPoint)point height:(float)height {
return CGPointMake(point.x, height - point.y);
}
@end

"必须有 50 个声誉才能评论">

您应该输出您尝试使用的每个变量。改变NSLog(@"[myLOG] ciImage");NSLog(@"[myLOG] ciImage %@", ciImage);

如果它打印出NSLog(@"[myLOG] ciImage");那么崩溃必须在那之后和下一个NSLog之前发生。这意味着此行失败:

[colorControlsFilter setValue:ciImage forKey:@"inputImage"];

正如您所说,图像是nil这意味着设置图像的行包含错误。

ciImage = [ciImage imageByApplyingFilter:@"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates];

过滤器是否存在并且编写正确?
出于调试目的,您可以取消注释此行并查看是否显示原始图像。

最新更新