0

CGContextDrawImage は非常にコストがかかる可能性があるため、ピクセル データを調べる間、与えるデータの量を最小限に抑えようとしています。2 つの画像とその交点の CGRect がある場合、CGContextDrawImage を取得して、それぞれの交点のみを個別に描画することはできますか (その結果、画像の 1 つの交点を含む 2 つの CGContextRef が生成されます)。

動作しないコードがいくつかありますが、1 つの画像に必要なものに近いはずです...

    CGImageRef imageRef = [image CGImage];
    NSUInteger width = rectIntersect.size.width;
    NSUInteger height = rectIntersect.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    // rectIntersect down here contains the intersection of the two images...
    CGContextDrawImage(context, rectIntersect, imageRef);
4

1 に答える 1

2

まあ、本当に一緒に欲しいのでなければ、描く必要はありませんCGBitmapContextCGImageCreateWithImageInRect()サブイメージを作成するために使用します。これは、フレームワークが画像データをコピーすることを必ずしも必要としません。オリジナルの画像データを参照するだけかもしれません。したがって、非常に効率的です。

コンテキストに描画された画像が本当に必要な場合は、もちろんサブ画像を描画することもできます。

于 2012-04-29T17:09:00.593 に答える