简体   繁体   中英

Issues passing UIImage after using CIFilter

I have a problem that I can't seem to solve or find any SO related posts. I have an image I'm trying to pass into a function, edit with CIFilter and pass back the result for display. 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 .

You have two options:

Option 1

Return the modified image:

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

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

And in your viewDidLoad:

UIImage *imout = [self editimage:image];

Option 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:

[self editimage: image fpin:&imout];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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