如何从CIImage获取CGBItmapContextCreate

时间:2014-10-28 13:55:03

标签: ios ios7

我在使用UISlider时应用了以下过滤器。

- (IBAction)exposureSliderChanged:(id)sender {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    exposureFilter = [CIFilter filterWithName: @"CIExposureAdjust" keysAndValues: @"inputImage", aCIImage, nil];
    [exposureFilter setValue:[NSNumber numberWithFloat:_slider.value] forKey:@"inputEV"];
    exposureValue=_slider.value;

    dispatch_async(dispatch_get_main_queue(), ^{
        outputImage = [exposureFilter outputImage];
        CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
        _modifiedImage = [UIImage imageWithCGImage:cgimg scale:2.0 orientation:UIImageOrientationUp];
        [_cropImageView setImage:_modifiedImage];
        CGImageRelease(cgimg);
    });

;


});

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    exposureDict = [NSDictionary dictionaryWithObjectsAndKeys:
                    [NSNumber numberWithFloat:(float)_slider.value],@"Exposure", nil];
});

}

我希望能够获取创建的图像的原始数据,以便应用棕褐色滤镜。 棕褐色过滤器的代码:

- (IBAction)sepiaFilterButtonPressed:(id)sender {

_slider.hidden=YES;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    _undoImage=[_modifiedImage copy];

    SepiaToneEffect *sepiaToneEffect = [[SepiaToneEffect alloc] init];
    [sepiaToneEffect setImageWidth: imageWidth];
    [sepiaToneEffect setImageHeight: imageHeight];

    //set the blurred image to your imageView in the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"%f",_modifiedImage.size.width);

        _modifiedImage = [sepiaToneEffect modifyImageWithSepia:_modifiedImage
                                                  imageRawData:imageRawData
                                                    withColorR:112
                                                        colorG:66
                                                        colorB:20
                                                         depth:0.5];
        _modifiedImage=[Globals imageWithImage:_modifiedImage scaledToSize:sizeNeeded];

        NSLog(@"%f",_modifiedImage.size.width);
        [_cropImageView setImage:_modifiedImage];
        //Create CIImage
        UIImage *aUIImage =_modifiedImage;
        CGImageRef aCGImage = aUIImage.CGImage;
        aCIImage = [CIImage imageWithCGImage:aCGImage];

        //Create context
        context = [CIContext contextWithOptions:nil];

    });

    NSDictionary *modification = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], @"Sepia",nil];
    [[kAppDelegate modificationsArray] addObject:modification];

});

}

是否有方法将过滤器应用于过滤器?例如:  1 - 应用曝光滤镜;  2 - 在涂抹曝光后的图像上涂抹棕褐色。

1 个答案:

答案 0 :(得分:0)

我设法在过滤器上使用过滤器(CIFilter)(使用CGContextCreate绘制位)在滑块的方法上编写此代码( exposureSliderChanged:(id)sender ):

 imageRawData = realloc(imageRawData,CGImageGetHeight(_modifiedImage.CGImage)*CGImageGetWidth(_modifiedImage.CGImage)*4);

        localData = CGBitmapContextCreate(imageRawData,
                                          imageWidth,
                                          imageHeight,
                                          bitsPerComponent,
                                          bytesPerRow,
                                          colorSpace,
                                          kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);


        CGContextDrawImage(localData, CGRectMake(0, 0, imageWidth, imageHeight),cgimg);
        CGContextRelease(localData);
        CFRelease(localData);