首页 > 解决方案 > 尝试从 objects[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

标签: iosobjective-creact-native

解决方案


“必须有 50 声望才能发表评论”

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

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

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

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

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

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


推荐阅读