2011-12-03 9 views
6

He hecho esto debajo de la función fitImage que toma un UIImage y un CGSize. En caso de que la imagen de entrada sea más grande que el cuadro de entrada, la imagen se reducirá para completarla en el cuadro. Si el cuadro y la imagen no tienen la misma proporción, la imagen no llena el cuadro por completo, y habría algunos fondos visibles. En este caso, este fondo es siempre blanco. ¿Cómo puedo cambiar esto a, p. un fondo negro?UIImage: fondo de relleno

+ (UIImage*) fitImage:(UIImage*)image inBox:(CGSize)size { 

    if (image.size.width==size.width && image.size.height==size.height) 
     return image; 

    if (image.size.width<size.width && image.size.height<size.height) 
     return [Util scaleImage:image toSize:size]; 

    float widthFactor = size.width/image.size.width; 
    float heightFactor = size.height/image.size.height; 

    CGSize scaledSize = size; 
    if (widthFactor<heightFactor) { 
     scaledSize.width = size.width; 
     scaledSize.height = image.size.height * widthFactor; 
    } else { 
     scaledSize.width = image.size.width * heightFactor; 
     scaledSize.height = size.height; 
    } 

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

    float marginX = (size.width-scaledSize.width)/2; 
    float marginY = (size.height-scaledSize.height)/2; 
    CGRect scaledImageRect = CGRectMake(marginX, marginY, scaledSize.width, scaledSize.height); 

    [image drawInRect:scaledImageRect]; 

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return scaledImage; 
} 

Por favor, dime si necesitas más información.

Solución:

+ (UIImage*) fitImage:(UIImage*)image inBox:(CGSize)size withBackground:(UIColor*)color { 

    if (image.size.width==size.width && image.size.height==size.height) 
     return image; 

    if (image.size.width<size.width && image.size.height<size.height) 
     return [Util scaleImage:image toSize:size]; 

    float widthFactor = size.width/image.size.width; 
    float heightFactor = size.height/image.size.height; 

    CGSize scaledSize = size; 
    if (widthFactor<heightFactor) { 
     scaledSize.width = size.width; 
     scaledSize.height = image.size.height * widthFactor; 
    } else { 
     scaledSize.width = image.size.width * heightFactor; 
     scaledSize.height = size.height; 
    } 

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

    float marginX = (size.width-scaledSize.width)/2; 
    float marginY = (size.height-scaledSize.height)/2; 
    CGRect scaledImageRect = CGRectMake(marginX, marginY, scaledSize.width, scaledSize.height); 

    UIImage* temp = UIGraphicsGetImageFromCurrentImageContext(); 
    [color set]; 
    UIRectFill(CGRectMake(0.0, 0.0, temp.size.width, temp.size.height)); 
    [image drawInRect:scaledImageRect]; 

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return scaledImage; 
} 

Respuesta

22

Puede utilizar UIRectFill(rect) para rellenar el fondo de un marco de mapa de bits con el color de relleno actual. Para configurar el color de relleno, puede usar [UIColor establecer].

p. Ej.

//Get a temp image to determine the size of the bitmap context 
UIImage* temp = UIGraphicsGetImageFromCurrentImageContext(); 
[[UIColor redColor] set]; //set the desired background color 
UIRectFill(CGRectMake(0.0, 0.0, temp.size.width, temp.size.height)); //fill the bitmap context 
[image drawInRect:scaledImageRect]; //draw your image over the filled background 
+0

Gracias weichsel! – dhrm

Cuestiones relacionadas