我正在创建一个拼贴画,结合了其他图像的元素。这是一些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图像也颠倒了。

最佳答案

CGContextClipToRect与提供的参数与当前剪切矩形相交。因此,第二次调用它时,实际上是将剪切区域变为零。

如果不还原图形状态,则无法还原裁剪区域。因此,在循环的顶部调用CGContextSaveGState,在底部调用CGContextRestoreGState

可以通过调整当前转换矩阵来固定上下颠倒部分:调用CGContextTranslateCTM移动原点,然后调用CGContextScaleCTM翻转y轴。

关于iphone - 多个剪辑 react 在核心图形中创建拼贴,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2234668/

10-13 04:27