1

私のアプリでは、コアイメージフィルターを使用しています。

私のコード:

- (UIImage*)applyRGBWithRed:(float)red withGreen:(float)green withBlue:(float)blue
{
        ciimage=[CIImage imageWithCGImage:self.CGImage];
       // Make the filter
    CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"]; // 2
    [colorMatrixFilter setDefaults]; // 3
    [colorMatrixFilter setValue:ciimage forKey:kCIInputImageKey]; // 4
    [colorMatrixFilter setValue:[CIVector vectorWithX:red Y:0 Z:0 W:0] forKey:@"inputRVector"]; // 5
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:green Z:0 W:0] forKey:@"inputGVector"]; // 6
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:blue W:0] forKey:@"inputBVector"]; // 7
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8z


    // Get the output image recipe
    CIImage *outputImage = [colorMatrixFilter outputImage];  // 9

    // Create the context and instruct CoreImage to draw the output image recipe into a CGImage
    context = [CIContext contextWithOptions:nil];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; // 10
    self.saveImage=[UIImage imageWithCGImage:cgimg];
    CGImageRelease(cgimg);
    return self.saveImage;
  }

大きな画像(つまり、1.4 MBを超えるサイズ)を処理しているときに、メモリ警告が表示され、アプリがクラッシュします。

なぜクラッシュにつながるのですか?このシナリオをどのように管理できますか?

どんなアドバイスやアイデアも高く評価できます。私はこれで一週間以上打った...

前もって感謝します....

プロファイラーで実行している間、それは次の出力を与えます ここに画像の説明を入力してください

4

1 に答える 1

2

大きな画像を小さな部分に「分割」し、それぞれにファイラーを適用して、それらを組み合わせて大きな画像に戻すことができるかもしれません。

UIImage *image = [UIImage imageWithContentsOfFile:imagePath]; // The Big image
NSMutableArray *croppedImages = [NSMutableArray array];
CGSize imageSize = image.size;
CGSize cropSize = CGSizeMake(500, 500); // The size of each "small image"
// Step 1: Produce the small images and save to disk
for (CGFloat x = 0; x < imageSize.width; x += cropSize.width)
{
    for (CGFloat y = 0; y < imageSize.height; y += cropSize.height)
    {
        UIGraphicsBeginImageContext(cropSize);
        [image drawAtPoint:CGPointMake(-x, -y)];
        UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"cropped_%f_%f.png",x,y]];
        NSData *imageData = UIImagePNGRepresentation(cropped);
        [imageData writeToFile:path atomically:YES];
        imageData = nil;

        [croppedImages addObject:path];
    }
}
// Release the big image (assume we are using ARC)
image = nil;
// Step 2: Apply the filter to the small images and re-save them
for (NSString *path in croppedImages)
{
    UIImage *img = [UIImage imageWithContentsOfFile:path];

    // Apply your filter to img here

    NSData *imageData = UIImagePNGRepresentation(img);
    [imageData writeToFile:path atomically:YES];
    imageData = nil;
}
// Step 3: Combine the small images back to a big one
UIGraphicsBeginImageContext(imageSize);
for (CGFloat x = 0; x < imageSize.width; x += cropSize.width)
{
    for (CGFloat y = 0; y < imageSize.height; y += cropSize.height)
    {
        NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"cropped_%f_%f.png",x,y]];
        UIImage *img = [UIImage imageWithContentsOfFile:path];

        [img drawAtPoint:CGPointMake(x, y)];
    }
}
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Step 4: Optionally remove the temporary files
for (NSString *path in croppedImages)
{
    [[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
}

明らかに、それは遅くなります。

于 2012-12-13T04:58:54.527 に答える