6

他の画像の要素と組み合わせたコラージュを作成しています。これが私がしていることを説明するためのいくつかのASCIIアートです:

Given images A, B and C,
AAA, BBB, CCC
AAA, BBB, CCC
AAA, BBB, CCC

I take part of A, part of B and part of C as columns:

Axx, xBx, xxC
Axx, xBx, xxC
Axx, xBx, xxC

...and combine them in one image like this:

ABC
ABC
ABC

where the first 1/3rd of the image is a colum of A's pic, the middle is a column of B's pic and the last is a column of C's pic.

私はいくつかのコードを書いていますが、それは最初の列だけを示しており、残りは示していません…どういうわけかクリッピングをクリアする必要があると思います。

+ (UIImage *)collageWithSize:(NSInteger)size fromImages:(NSArray *)images {
    NSMutableArray *selectedImages = [NSMutableArray array];
    [selectedImages addObjectsFromArray:images];

    // use the selectedImages for generating the thumbnail
    float columnWidth = (float)size/(float)[selectedImages count];

    //create a context to do our clipping in
    UIGraphicsBeginImageContext(CGSizeMake(size, size));
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    for (int i = 0; i < [selectedImages count]; i++) {
        // get the current image
        UIImage *image = [selectedImages objectAtIndex:i];

        //create a rect with the size we want to crop the image to
        CGRect clippedRect = CGRectMake(i*columnWidth, 0, columnWidth, size);
        CGContextClipToRect(currentContext, clippedRect);

        //create a rect equivalent to the full size of the image
        CGRect drawRect = CGRectMake(0, 0, size, size);

        //draw the image to our clipped context using our offset rect
        CGContextDrawImage(currentContext, drawRect, image.CGImage);
    }

    //pull the image from our cropped context
    UIImage *collage = UIGraphicsGetImageFromCurrentImageContext();

    //pop the context to get back to the default
    UIGraphicsEndImageContext();

    //Note: this is autoreleased
    return collage;
}

私は何が間違っているのですか?

PS画像も逆さまに描かれています。

4

2 に答える 2

9

CGContextClipToRect指定された引数で現在のクリッピング四角形と交差します。したがって、2回目に呼び出すと、クリッピング領域が事実上何もなくなります。

グラフィックス状態を復元せずにクリッピング領域を復元する方法はありません。したがって、CGContextSaveGStateループの一番上で を呼び出しCGContextRestoreGState、一番下で を呼び出します。

逆さまの部分は、現在の変換行列を調整することで修正できます。呼び出しCGContextTranslateCTMて原点を移動し、次にCGContextScaleCTMy 軸を反転します。

于 2010-02-10T05:53:47.880 に答える
0

上下逆さまの画像は、以下のメソッドを呼び出すことで修正できます。

CGImageRef flip (CGImageRef im) {
CGSize sz = CGSizeMake(CGImageGetWidth(im), CGImageGetHeight(im));
UIGraphicsBeginImageContextWithOptions(sz, NO, 0);
CGContextDrawImage(UIGraphicsGetCurrentContext(),
                   CGRectMake(0, 0, sz.width, sz.height), im);
CGImageRef result = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
UIGraphicsEndImageContext();
return result;

}

このコードをどこに配置するかについては、以下のコードを参考にしてください。

UIGraphicsBeginImageContextWithOptions(CGSizeMake(leftRect.size.width, leftRect.size.height), NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, leftRect,flip(leftReference));
于 2012-12-04T07:20:36.833 に答える