Pregunta

Estoy tratando de descubrir cómo cambiar el color/tono de una UIImage.Descubrí que iOS5 tiene muchos filtros de imagen, pero me cuesta encontrar documentación sobre el uso adecuado de ellos. CIColorMatrix filtrar.

-(void)doCIColorMatrixFilter
{

    //does not work, returns nil image
    CIImage* inputImage = [CIImage imageWithCGImage:[[UIImage imageNamed:@"button.jpg"]CGImage]];

    CIFilter *myFilter;
    NSDictionary *myFilterAttributes;
    myFilter = [CIFilter filterWithName:@"CIColorMatrix"];
    [myFilter setDefaults];

    myFilterAttributes = [myFilter attributes];

    [myFilterAttributes setValue:inputImage forKey:@"inputImage"];
    //How to set up attributes?

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *ciimage = [myFilter outputImage];
    CGImageRef cgimg = [context createCGImage:ciimage fromRect:[ciimage extent]];
    UIImage *uimage = [UIImage imageWithCGImage:cgimg scale:1.0f orientation:UIImageOrientationUp];
    [imageView setImage:uimage];
    CGImageRelease(cgimg);

}

¿Qué código entra en el diccionario para este filtro?

¿Fue útil?

Solución

Un mes despues ...

Esto es un ejemplo de CIColorMatrix configurando todos sus parámetros :)

-(void)doCIColorMatrixFilter
{
    // Make the input image recipe
    CIImage *inputImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"facedetectionpic.jpg"].CGImage]; // 1

    // Make the filter
    CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"]; // 2
    [colorMatrixFilter setDefaults]; // 3
    [colorMatrixFilter setValue:inputImage forKey:kCIInputImageKey]; // 4
    [colorMatrixFilter setValue:[CIVector vectorWithX:1 Y:1 Z:1 W:0] forKey:@"inputRVector"]; // 5
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:1 Z:0 W:0] forKey:@"inputGVector"]; // 6
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:1 W:0] forKey:@"inputBVector"]; // 7
    [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8

    // Get the output image recipe
    CIImage *outputImage = [colorMatrixFilter outputImage];  // 9

    // Create the context and instruct CoreImage to draw the output image recipe into a CGImage
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; // 10

    // Draw the image in screen
    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:cgimg]];
    CGRect f = imageView2.frame;
    f.origin.y = CGRectGetMaxY(imageView.frame);
    imageView2.frame = f;

    [self.view addSubview:imageView2];
}

Entonces esto es lo que hace la muestra:

En 1 Creamos el ciimage, si obtiene cero allí, asegúrese de pasar la UIImage/CGImage o la ruta correcta.

En 2 crea el filtro, ya lo sabes :)

En 3 establezca los parámetros del filtro en sus valores predeterminados, aunque la guía de programación CoreImage sugiere que deberíamos hacer esto (no he experimentado ninguna cosa extraña/mala si se evita).

En 4 establecer la imagen ci de entrada

De 5 a través de 8 establecemos los parámetros.Por ejemplo, hice el vector rojo {1,1,1,0} para que la imagen se vea rojizo. 6, 7 y 8 y no es necesario aquí ya que sus valores son los mismos que los predeterminados (recuerde que llamamos -setDefaults?) pero para fines educativos supongo que están bien :)

En 9 establece la imagen de salida, aunque aún no está dibujada.

Finalmente en 10 le dice a CoreImage que dibuje la imagen de salida en un CGImage, y colocamos ese CGImage en un UIImage y dentro de un UIImageView.

Este es el resultado (usé la misma imagen que este tutorial):

Reddish

Espero eso ayude.

Otros consejos

Solo un ejemplo de Swift con una cadena para agregar a la respuesta anterior de nacho4d

var output = CIFilter(name: "CIColorControls")
output.setValue(ciImage, forKey: kCIInputImageKey)
output.setValue(1.8, forKey: "inputSaturation")
output.setValue(0.01, forKey: "inputBrightness")

filter = CIFilter(name: "CIColorMatrix")
filter.setDefaults()
filter.setValue(output.outputImage, forKey: kCIInputImageKey)
filter.setValue(CIVector(x: 1, y: 0.1, z: 0.1, w: 0), forKey: "inputRVector")
filter.setValue(CIVector(x: 0, y: 1, z: 0, w: 0), forKey: "inputGVector")
filter.setValue(CIVector(x: 0, y: 0, z: 1, w: 0), forKey: "inputBVector")

Tenga en cuenta que los valores predeterminados son 1,0,0 0,1,0 y 0,0,1 respectivamente para RGB y si desea hacerlo más rojo, debe configurar el inputRVector en 1,1,1 y dejar los demás iguales. (ya que esto multiplica los componentes azul y verde por los valores rojos)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top