1

当我将 CIFilters 应用于图像时,内存使用量不断增长,我不知道该怎么办。我已经尽我所能:

使用@autoreleasepool:

- (UIImage *)applySepiaToneTo:(UIImage *)img //Sepia
{
    @autoreleasepool
    {
        CIImage *ciimageToFilter = [CIImage imageWithCGImage:img.CGImage];
        CIFilter *sepia = [CIFilter filterWithName:@"CISepiaTone"
                                     keysAndValues: kCIInputImageKey, ciimageToFilter,
                                                    @"inputIntensity", @1.0, nil];

        return [self retrieveFilteredImageWithFilter:sepia];
    }
}

- (UIImage *)retrieveFilteredImageWithFilter:(CIFilter *)filtro
{
    @autoreleasepool
    {
        CIImage *ciimageFiltered = [filtro outputImage];

        CGImageRef cgimg = [_context createCGImage:ciimageFiltered
                                          fromRect:[ciimageFiltered extent]];

        UIImage *filteredImage = [UIImage imageWithCGImage:cgimg];

        CGImageRelease(cgimg);

        return filteredImage;
    }
}

我还在缩小要过滤的图像并在后台线程中进行过滤:

- (void)filterWasSelected:(NSNotification *)notification
{
    self.darkeningView.alpha = 0.5;
    self.darkeningView.userInteractionEnabled = YES;
    [self.view bringSubviewToFront:self.darkeningView];
    [self.activityIndic startAnimating];
    [self.view bringSubviewToFront:self.activityIndic];

    int indice = [notification.object intValue];
    __block NSArray *returnObj;
    __block UIImage *auxUiimage;

    if(choosenImage.size.width == 1280 || choosenImage.size.height == 1280)
    {
        UIImageView *iv;
        if(choosenImage.size.width >= choosenImage.size.height)
        {
            float altura = (320 * choosenImage.size.height)/choosenImage.size.width;
            iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,altura)];
            iv.image = choosenImage;
        }
        else
        {
            float largura = (choosenImage.size.width * 320)/choosenImage.size.height;
            iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,largura,320)];
            iv.image = choosenImage;
        }

        UIGraphicsBeginImageContextWithOptions(iv.bounds.size, YES, 0.0);
        [iv.layer renderInContext:UIGraphicsGetCurrentContext()];
        auxUiimage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    else
        auxUiimage = choosenImage;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        if(artisticCollection)
            returnObj = [self.filterCoordinator setupFilterArtisticType:indice toImage:auxUiimage];
        else
            returnObj = [self.filterCoordinator setupFilterOldOrVintageType:indice toImage:auxUiimage];

        dispatch_async(dispatch_get_main_queue(), ^{

            self.darkeningView.alpha = 0.3;
            self.darkeningView.userInteractionEnabled = NO;
            [self.activityIndic stopAnimating];
            [self.view bringSubviewToFront:stageBackground];
            [self.view bringSubviewToFront:stage];
            [self.view bringSubviewToFront:self.filtersContainerView];
            [self.view bringSubviewToFront:self.framesContainerView];
            [self.view bringSubviewToFront:self.colorsContainerView];

            if(returnObj)
            {
                auxUiimage = [returnObj firstObject];

                NSLog(@"filtered image width = %f and height = %f", auxUiimage.size.width, auxUiimage.size.height);

                returnObj = nil;
                choosenImageContainer.image = auxUiimage;
            }
        });
    });
}

我也尝试使用contextWithEAGLContext方法创建上下文,没有任何改变。我研究了很多,包括堆栈溢出,但一无所获。

直到我将图像放在图像视图中(图像来自相册)我只使用了 23 兆内存,当我应用过滤器时,使用量跳到 51 兆并且没有下降。如果我继续应用其他过滤器,内存使用只会增加。

我的应用程序中没有链接,我已签入 Instruments。另外,bringSubviewToFront 方法也不负责,我已经检查过了。它是在创建 CIImage 之后创建 CIFilter 对象。我知道在应用过滤器的过程中数据是加载到内存中的,但是应用过滤器后如何清理内存呢?

有什么我不知道的秘密吗??请帮忙

4

0 回答 0