【项目】用CIFilter得到图片没办法保存到沙盒里面,显示是CGImage为空

时间:2024-06-19 15:33:20

错误代码:

    if (iOS9)
    {
        CGImageRef cgImage = [[CIContext new] createCGImage:outputImage fromRect:outputImage.extent];
        imageTwo = [UIImage imageWithCGImage:cgImage];
    }
    else
    {
        CGImageRef cgImage = [[CIContext new] createCGImage:outputImage fromRect:outputImage.extent];
        CGContextRef contenxt = UIGraphicsGetCurrentContext();
        CGContextDrawImage(contenxt, outputImage.extent, cgImage);

        imageTwo = [UIImage imageWithCIImage:outputImage];
    }
    

错误原因:

1.没有有效的获得上下文

2.没有通过CGImage获得UIImage

处理:

- (CIContext *)context
{
    if (!_context)
    {
        _context = [CIContext contextWithOptions:nil];
    }
    return _context;
}
CIImage * inputImage = [[CIImage alloc] initWithImage:_image];
NSLog(@"%@",inputImage);

[_filter setValue:inputImage forKey:kCIInputImageKey];

CIImage * outputImage = [_filter outputImage];
NSLog(@"%@",outputImage);

CGImageRef cgImage = [self.context createCGImage:outputImage fromRect:outputImage.extent];
NSLog(@"%@",cgImage);

UIImage *imageTwo = [UIImage imageWithCGImage:cgImage];

打印结果:

-- :::60b] <CIPhotoEffectChrome: inputImage=nil>
-- :::60b] <CIImage:    ]>
-- :::60b] <CIImage:    ]>
-- :::60b] <CGImage 0x16ff0cf0>
-- :::60b] <UIImage: 0x16ff0c60>
-- :::60b] 存储图片:YES
-- :::60b] <CGImage 0x16ff0cf0>
-- :::60b] 存储图片:YES
-- :::60b] <CGImage 0x16d184b0>

推荐地址:http://www.cnblogs.com/kenshincui/p/3959951.html