简体   繁体   English

使用CIFilter后传递UIImage的问题

[英]Issues passing UIImage after using CIFilter

I have a problem that I can't seem to solve or find any SO related posts. 我有一个问题,我似乎无法解决或找到任何与SO相关的帖子。 I have an image I'm trying to pass into a function, edit with CIFilter and pass back the result for display. 我有一张要传递给函数的图像,使用CIFilter编辑并将结果传回以进行显示。 However, when the image is passed back it seems to have been released and I have no idea why. 但是,当图像传回时,它似乎已被释放,我也不知道为什么。 Here's the code: 这是代码:

 - (void)viewDidLoad {
    [super viewDidLoad];
    myImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIImage *image =  [UIImage imageNamed:@"testim.png"];

    UIImage *imout;
    [self editimage: image fpin:imout];
    myImageView.image = imout;
    [self.view addSubview:myImageView];

}


-(void) editimage:(UIImage *) image fpin:(UIImage *) img_new{
        CIImage *ciImage = [CIImage imageWithCGImage:[image CGImage]];

        CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"
                                      keysAndValues: kCIInputImageKey, ciImage,
                            @"inputIntensity", @0.8, nil];
        CIImage *outputImage = [filter outputImage];

        CIContext *context = [CIContext contextWithOptions:nil];
        CGImageRef cgimage = [context createCGImage:outputImage fromRect:[outputImage extent]];
        img_new = [UIImage imageWithCGImage:cgimage];
        CGImageRelease(cgimage);
    }

Can anyone shed some light on this? 谁能对此有所启发?

The problem with your code is that in fact it does not change your output image at all. 您的代码的问题在于,实际上它根本不会改变您的输出图像。 Your editimage method assigns an image to local copy of imout . editimage方法分配给的本地副本的图像imout

You have two options: 您有两种选择:

Option 1 选项1

Return the modified image: 返回修改后的图像:

-(UIImage*) editimage:(UIImage *) image {
    ...

    UIImage *newImage = [UIImage imageWithCGImage:cgimage];
    CGImageRelease(cgimage);
    return newImage
}

And in your viewDidLoad: 并在您的viewDidLoad中:

UIImage *imout = [self editimage:image];

Option 2 选项2

If you really want to stick with your original pattern, you can pass the reference to your image: 如果您确实想坚持使用原始图案,则可以将引用传递给图像:

-(void) editimage:(UIImage *) image fpin:(UIImage **) img_new {
    ...
    *img_new = [UIImage imageWithCGImage:cgimage];
    CGImageRelease(cgimage);
}

And in your viewDidLoad: 并在您的viewDidLoad中:

[self editimage: image fpin:&imout];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM