什么是关于Core Image给iOS9的"需要一个可以读取RGB8的swizzler"?

Wyt*_*the 6 core-image ios

首先,我想到了解决方案,但这不是一个好方法.我会在最后给出.

当我处理过滤器时iOS9我得到"需要一个swizzler以便RGB8可以被读取"错误信息并且返回图像是全黑的这种方法

[self.context createCGImage:self.outputImage fromRect:[self.outputImage extent]];

在这里

 - (UIImage *)fliterImage:(UIImage *)input flitername:(NSString *)name
{
    NSString * fliter_name = name;
    self.context = [CIContext contextWithOptions:nil]; 
    UIImage *image;
    if ([fliter_name isEqualToString:@"OriginImage"]){
        image = input;
    }else {

        self.ciImage = [[CIImage alloc] initWithImage:input];
        self.filter = [CIFilter filterWithName:fliter_name keysAndValues:kCIInputImageKey,self.ciImage, nil];
        [self.filter setDefaults];
        self.outputImage = [self.filter outputImage];
        // here give the error message
        self.cgimage = [self.context createCGImage:self.outputImage fromRect:[self.outputImage extent]];

        UIImage *image1 = [UIImage imageWithCGImage:self.cgimage];
        CGImageRelease(self.cgimage);

        self.context = [CIContext contextWithOptions:nil];
        //
        // self.filter=[CIFilter filterWithName:@"CIColorControls"];
        // _imageView.image=image;
        //
        //[self.filter setValue:[CIImage imageWithCGImage:image.CGImage] forKey:@"inputImage"];
        image = image1;
    }
    return image;
}
Run Code Online (Sandbox Code Playgroud)

而且这个婴儿车input是由我的导演写的这种方法创造的

-(UIImage *)UIImageFromBmp:(uint8_t**)pixels withPlanesCount:(uint32_t)planeCount
{
    uint8_t *rgb = malloc(sizeof(uint8_t)*1920*1080*3);
    int step = 1920 * 3;
    // CSU: YUV420 has 2 kinds of data structure
    //      planeCount==3 --> |yyyyyyyy|
    //                        |yyyyyyyy|
    //                        |uuuu|
    //                        |vvvv|
    //      planeCount==2 --> |yyyyyyyy|
    //                        |yyyyyyyy|
    //                        |uvuvuvuv|
    if (planeCount == 3) {
        for (int rows=0; rows<1080; rows++) {
            for (int cols=0; cols<1920; cols++) {
                int y = pixels[0][rows*1920 + cols];
                int u = pixels[1][(rows>>1)*960 + (cols>>1)];
                int v = pixels[2][(rows>>1)*960 + (cols>>1)];
                int r = (int)((y&0xff) + 1.402*((v&0xff)-128));
                int g = (int)((y&0xff) - 0.34414*((u&0xff)-128) - 0.71414*((v&0xff)-128));
                int b = (int)((y&0xff) + 1.772*((u&0xff)-128));
                rgb[rows*step + cols*3 + 0] = MAX(MIN(r, 255), 0);
                rgb[rows*step + cols*3 + 1] = MAX(MIN(g, 255), 0);
                rgb[rows*step + cols*3 + 2] = MAX(MIN(b, 255), 0);
            }
        }
    } else if(planeCount == 2) {
        for (int rows=0; rows<1080; rows++) {
            for (int cols=0; cols<1920; cols++) {
                int y = pixels[0][rows*1920 + cols];
                int u = pixels[1][(rows>>1)*1920 + ((cols>>1)<<1)];
                int v = pixels[1][(rows>>1)*1920 + ((cols>>1)<<1)+1];
                int r = (int)((y&0xff) + 1.402*((v&0xff)-128));
                int g = (int)((y&0xff) - 0.34414*((u&0xff)-128) - 0.71414*((v&0xff)-128));
                int b = (int)((y&0xff) + 1.772*((u&0xff)-128));
                rgb[rows*step + cols*3 + 0] = MAX(MIN(r, 255), 0);
                rgb[rows*step + cols*3 + 1] = MAX(MIN(g, 255), 0);
                rgb[rows*step + cols*3 + 2] = MAX(MIN(b, 255), 0);
            }
        }
    } else {
        // CSU: should not happen
        assert(0);
    }

    NSData *data = [NSData dataWithBytes:rgb length:step*1080];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

    // CSU: Creating CGImage from raw data
    CGImageRef imageRef = CGImageCreate(1920,                                       //width
                                        1080,                                       //height
                                        8,                                          //bits per component
                                        8 * 3,                                      //bits per pixel
                                        step,                                       //bytesPerRow
                                        colorSpace,                                 //colorspace
                                        kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
                                        provider,                                   //CGDataProviderRef
                                        NULL,                                       //decode
                                        false,                                      //should interpolate
                                        kCGRenderingIntentDefault                   //intent
                                        );


    // CSU: Getting UIImage from CGImage
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);

    free(rgb);

    return finalImage;
}
Run Code Online (Sandbox Code Playgroud)

我谷歌它需要一个swizzler,以便RGB8可以读取,并意识到它可能是图像格式导致问题.所以我打电话[self clipImageWithScaleWithsize:_scaledImage.size input:_sourceImage];给它在传递之前处理

[self.context createCGImage:self.outputImage fromRect:[self.outputImage extent]]; 用这个(大小只是图像大小)

- (UIImage *)clipImageWithScaleWithsize:(CGSize)asize input:(UIImage *)input
{
    UIImage *newimage;
    UIImage *image = input;
    if (nil == image) {
        newimage = nil;
    }
    else{
        CGSize oldsize = image.size;
        CGRect rect;
        if (asize.width/asize.height > oldsize.width/oldsize.height) {
            rect.size.width = asize.width;
            rect.size.height = asize.width*oldsize.height/oldsize.width;
            rect.origin.x = 0;
            rect.origin.y = (asize.height - rect.size.height)/2;
        }
        else{
            rect.size.width = asize.height*oldsize.width/oldsize.height;
            rect.size.height = asize.height;
            rect.origin.x = (asize.width - rect.size.width)/2;
            rect.origin.y = 0;
        }
        UIGraphicsBeginImageContext(asize);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextClipToRect(context, CGRectMake(0, 0, asize.width, asize.height));
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
        [image drawInRect:rect];
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return newimage;
}
Run Code Online (Sandbox Code Playgroud)

它确实解决了这个问题,但我仍然感到困惑,我不认为这是一个好方法.什么"需要一个swizzler才能读取RGB8"真的意味着什么,为什么我的解决方案有效?