2010-02-10 12 views
6

Estoy creando un collage combinado con elementos de otras imágenes. Aquí es un poco de arte ASCII para explicar lo que estoy haciendo:Rectos múltiples recortados para crear collage en gráficos básicos

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. 

He escrito algo de código, pero sólo muestra la primera columna y no el resto ... yo creo que tenga que limpiar el recorte de alguna manera, bt soy no estoy seguro de cómo hacerlo o si este es incluso el mejor enfoque.

+ (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; 
} 

¿Qué estoy haciendo mal?

PD La imagen también se dibuja boca abajo.

Respuesta

9

CGContextClipToRect interseca el rectángulo de recorte actual con el argumento proporcionado. Entonces, la segunda vez que lo llamas, efectivamente estás convirtiendo tu región de recorte en nada.

No hay forma de restaurar la región de recorte sin restaurar el estado de los gráficos. Por lo tanto, realice una llamada al CGContextSaveGState en la parte superior de su ciclo y una llamada al CGContextRestoreGState en la parte inferior.

La parte invertida puede ser fijado mediante el ajuste de la matriz de transformación actual: llamar CGContextTranslateCTM para mover el origen y luego CGContextScaleCTM para voltear el eje y.

+1

' CGContextSaveGState' y 'CGContextRestoreGState' son lo que estaba buscando. ¡Gracias! –

0

La imagen al revés se puede corregir mediante una llamada al método siguiente:

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; 

}

tomar la ayuda desde abajo código en cuanto a donde u será poner este código:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(leftRect.size.width, leftRect.size.height), NO, 0); 
CGContextRef con = UIGraphicsGetCurrentContext(); 
CGContextDrawImage(con, leftRect,flip(leftReference)); 
Cuestiones relacionadas