Несколько отсеченный Rects для создания коллажа в базовой графике

Я создаю коллаж, объединенный с элементами из других изображений. Вот некоторое 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 изображение тянет вверх тормашками также.

6
задан coneybeare 10 February 2010 в 05:35
поделиться

1 ответ

CGContextClipToRect пересекает текущий прямоугольник отсечения с предоставленным аргументом. Таким образом, во второй раз, когда вы его вызываете, вы фактически превращаете область отсечения в ничто.

Невозможно восстановить область отсечения без восстановления графического состояния. Итак, вызовите CGContextSaveGState в верхней части цикла и вызовите CGContextRestoreGState внизу.

Перевернутая часть может быть исправлена ​​путем настройки текущей матрицы преобразования: вызовите CGContextTranslateCTM , чтобы переместить начало координат, а затем CGContextScaleCTM , чтобы перевернуть ось y.

9
ответ дан 10 December 2019 в 02:47
поделиться